Cách tiếp cận tốt nhất để triển khai CRUD trên giao diện BL sử dụng giao diện DAL là gì? Tôi cần kẻ ý kiến của bạn ..Thực hiện CRUD bằng Giao diện
Đây là dự thảo của tôi ..
Đối tượng dữ liệu được ánh xạ trong bảng cơ sở dữ liệu
public class Student
{
public string StudentId { get; set; }
public string StudentName { get; set; }
public Course StudentCourse { get; set; }
}
public class Course
{
public string CourseCode { get; set; }
public string CourseDesc { get; set; }
}
Tôi tạo ra một giao diện CRUD đến hoạt động của đối tượng trừu tượng
public interface IMaintanable<T>
{
void Create(T obj);
T Retrieve(string key);
void Update(string key);
void Delete(string key);
}
Sau đó, một thành phần quản lý Thực thể và các hoạt động của nó bằng cách triển khai giao diện
public class StudentManager : IMaintainable<Student>
{
public void Create(Student obj)
{
// inserts record in the DB via DAL
}
public Student Retrieve(string userId)
{
// retrieveds record from the DB via DAL
}
public void Update()
{
// should update the record in the DB
}
public void Delete(string userId)
{
// deletes record from the DB
}
}
sử dụng mẫu
public void Button_SaveStudent(Event args, object sender)
{
Student student = new Student()
{
StudentId = "1", StudentName = "Cnillincy"
}
new StudentManager().Create(student);
}
như bạn có thể thấy, có khá một bất thường về phương pháp cập nhật
public void Update()
{
// should update the record in the DB
}
gì nên phương pháp này phải cập nhật các đối tượng sở hữu? tôi có nên kế thừa Sinh viên không?
public class StudentManager : Student , IMaintainable<Student>
{
public void Update()
{
//update record via DAL
}
}
public void Button_SaveStudent(Event args, object sender)
{
Student student = new StudentManager();
student.StudentId = "1";
student.StudentName = "Cnillincy"
student.Update()
}
Hoặc tôi chỉ nên chứa lớp Sinh viên làm thuộc tính của Người quản lý sinh viên?
public class StudentManager : IMaintainable<Student>
{
public Student student { get; private set };
public void Create() {}
public void Update() {}
public void Retrieve() {}
public void Delete() {}
}
Điều gì phù hợp hơn? Còn giao diện thì sao? Bất kỳ kẻ đề xuất nào khác? thanks..C
bạn có nghĩ nó hiệu quả hơn kế thừa đối tượng không, vì nó có nghĩa là bạn sẽ tạo một bản sao khác của obj thay vì chỉ cập nhật nó, – CSharpNoob
Thừa kế sẽ không có ý nghĩa. Sinh viên của bạn KHÔNG PHẢI là sinh viên. – Matthieu
Điều Matthieu nói. Hãy suy nghĩ của học sinh như một hồ sơ và StudentManager của bạn giống như thư ký hiệu trưởng, gõ các hồ sơ lên khi họ bước vào. Nguyên tắc trách nhiệm duy nhất FTW. – Lunivore