实体框架、IRepository和UnitOfWork.如何实现DAL ?

本文关键字:实现 DAL 何实现 框架 IRepository UnitOfWork 实体 | 更新日期: 2023-09-27 18:17:39

使用EF的Repository的规范实现如下:

public interface IStudentRepository : IDisposable
{
    IEnumerable<Student> GetStudents();
    Student GetStudentByID(int studentId);
    void InsertStudent(Student student);
    void DeleteStudent(int studentID);
    void UpdateStudent(Student student);
    void Save();
}

这里我看到了IRepository和UnitOWork的混合。

但是Fowler说存储库是访问领域对象的类似集合的接口。据此,Update、Delete和Insert方法应该移动到另一个类中。并且Save应该移动到实现IUnitOfWork的类中。

在我当前的项目中,我们像官方文档所说的那样实现IRepository。将来会造成问题吗?一种解决方案是实现CQRS,可能使用事件源,但这将花费时间和资源。那么,如何在您的项目中实现DAL呢?

实体框架、IRepository和UnitOfWork.如何实现DAL ?

你的东西没什么问题。

我做的唯一额外的事情是为我的存储库创建一个单例工厂,它是DataService类的一部分。datasservice类将交付存储库实例