سلام استاد وقت بخیر
شما در پروژه تاپ لرن برای ویرایش و حذف دانشجوبان در دوره راهنمایی کردید که راه های دیگری هم هست بنده سعی کردم یک راه بهتر بنویسم که کمتر به سرور فشار بیاد و بیشتر در خود برنامه این موضوع حل بشه.
برای ویرایش دانشجویان در دروره شما تمام اطلاعات دیتابیس را پاک کردید و دوباره با تغییرات ذخیره کردید
من تغیرات را در برنامه ذخیره کردم و بعد با اطلاعات دیتابیس مقایسه و در نهایت آنهایی که حذف شدن را حذف و آنهایی که اضافه شدن را فقط اضافه کردم .
در هر حال تعدادخطوط کد نویسی افزایش پیدا کرد و فکر کنم به سرور در صورتی که پروژه روزی بزرگ شود فشار کمتری وارد میشود. متشکر م اگر شما و دوستان عزیز نظر و پیشنهادتون را بیان کنید
-- ابتدا یک viewmodel به صورت زیر :
namespace DataLayer.ViewModels
{
public class StudentInCourseViewModel
{
public int Id { get; set; }
public int PersonId { get; set; }
public string FullName { get; set; }
}
}یک property به نام List<int> _removedStudent; تعریف کردم تا آیدی دانشجویان حذف شده را در خود ذخیره کند و در متد کنترل شده اگر آیدی 0 باشد یعنی این دانشجو در دیتابیس هنوز ذخیره نشده و نیاز نیست به این لیست اضافه شود. و هنگام زدن دکمه ویرایش یک foreach رو این لیست قرار میگیرد و تک تک اعضای این لیست را طبق repository که برای پاک کردن نوشته شده حذف میکند.
در ضمن هنگام لود برنامه کل اطلاعات دانشجویان حاضر در دوره بجای اینکه مستقیم وارد datagridview شود ابتدا در یک viewmodel ذخیره میشود و سپس datagridview از روی این viewmodel پر میشود.
درصورت اضافه کردن شخصی به دوره آن شخص به لیست بالا اضافه میشود و هنگام ذخیره نهایی اشخاصی که id آنها صفر می باشد فقط در دوره اضافه میشود. کل behindCode فرم AddOrEditClass رو در ادامه قرار دادم.
using DataLayer.Models;
using DataLayer.Repositories.Contracts;
using DataLayer.Repositories.Repositories;
using DataLayer.ViewModels;
using System.Data;
namespace Toplearn_School.Classes
{
public partial class frmAddOrEditClass : Form
{
IPersonRepository _personRepository;
ICoursesRepository _coursesRepository;
List<StudentInCourseViewModel> _studentsInCourse;
List<int> _removedStudent;
public int _classId = 0;
public frmAddOrEditClass()
{
_personRepository = new PersonRepository();
_coursesRepository = new CoursesRepository();
_studentsInCourse = new List<StudentInCourseViewModel>();
_removedStudent = new List<int>();
InitializeComponent();
}
private void btnCancle_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmAddOrEditClass_Load(object sender, EventArgs e)
{
LoadStudent();
LoadTeacher();
if (_classId != 0)
{
BindForm(_classId);
}
}
void LoadStudent()
{
dgvPersons.AutoGenerateColumns = false;
dgvPersons.DataSource = _personRepository.GetAllPerson();
}
void BindForm(int classId)
{
var course = _coursesRepository.GetCourseById(classId);
txtClassHours.Text = course.Hours.ToString();
txtDescription.Text = course.Description;
txtStudentCount.Text = course.StudentCount.ToString();
txtTitle.Text = course.Title;
cboTeacher.SelectedValue = course.TeacherId;
this.Text = "ویرایش کلاس";
btnSave.Text = "ویرایش";
LoadStudentInClass();
bindStudentGrid();
}
void LoadStudentInClass()
{
dgvStudentInClass.AutoGenerateColumns = false;
_studentsInCourse = _coursesRepository.GetAllStudentInCourseList(_classId)
.Select(s => new StudentInCourseViewModel
{
Id = s.Id,
PersonId = s.PersonId,
FullName = s.person.Name + " " + s.person.Family
}).ToList();
}
void bindStudentGrid()
{
dgvStudentInClass.Rows.Clear();
foreach (var student in _studentsInCourse)
{
dgvStudentInClass.Rows.Add(student.PersonId, student.FullName);
}
}
void LoadTeacher()
{
var person = _personRepository.GetAllTeachers();
cboTeacher.DataSource = person;
cboTeacher.DisplayMember = "Name";
cboTeacher.ValueMember = "PersonId";
}
private void btnSave_Click(object sender, EventArgs e)
{
Course _course = new Course()
{
Title = txtTitle.Text,
Hours = Convert.ToInt32(txtClassHours.Text),
TeacherId = Convert.ToInt32(cboTeacher.SelectedValue.ToString()),
Description = txtDescription.Text,
StudentCount = Convert.ToInt32(txtStudentCount.Text)
};
if (_classId == 0)
{
_coursesRepository.CreateCourse(_course);
_coursesRepository.Save();
}
else
{
_course.CourseId = _classId;
_coursesRepository.UpdateCourse(_course);
if(_removedStudent.Count > 0)
{
foreach(var item in _removedStudent)
{
_coursesRepository.DeleteStudentInClass(item);
}
}
}
if (_studentsInCourse.Count > 0)
{
var newStudent = _studentsInCourse.Where(s=>s.Id==0).ToList();
foreach (var student in newStudent)
{
StudentInCourse _student = new StudentInCourse()
{
PersonId = student.PersonId,
CourseId = _course.CourseId,
RegisterDate = DateTime.Now
};
_coursesRepository.CreateStudentInClass(_student);
}
}
_coursesRepository.Save();
this.Close();
}
private void btnAddToClass_Click(object sender, EventArgs e)
{
if (dgvPersons.CurrentRow != null)
{
StudentInCourseViewModel student = new StudentInCourseViewModel();
student.PersonId = Convert.ToInt32(dgvPersons.CurrentRow.Cells[0].Value.ToString());
student.FullName = dgvPersons.CurrentRow.Cells[1].Value.ToString();
_studentsInCourse.Add(student);
bindStudentGrid();
}
}
private void btnRemoveStudent_Click(object sender, EventArgs e)
{
if (dgvStudentInClass.CurrentRow != null)
{
int studentId = Convert.ToInt32(dgvStudentInClass.CurrentRow.Cells[0].Value.ToString());
var removingStudent = _studentsInCourse.Where(s => s.PersonId == studentId).FirstOrDefault();
if (removingStudent.Id != 0)
{
_removedStudent.Add(removingStudent.Id);
}
_studentsInCourse.Remove(removingStudent);
}
bindStudentGrid();
}
}
}و دو دستور اضافه شده در _courseRepository :
public StudentInCourse GetStudentInClassById(int studentid)
{
return _Context.StudentInCourses.AsNoTracking().Where(s => s.Id == studentid).SingleOrDefault();
}
public void DeleteStudentInClass(int studentid)
{
var item = GetStudentInClassById(studentid);
_Context.StudentInCourses.Remove(item);
}
با تشکر از زحمات شما استاد عزیز
سلام
بسیار عالی