繁体   English   中英

带有存储库和实体框架的UnitOfWork

[英]UnitOfWork with Repository and Entity Framework

我正在使用Entity Framework进行项目,其中我已经实现了存储库模式和DI(Microsoft Unity),现在要维护数据库事务,我想实现UnitOfWork模式,但是我对如何在当前版本中实现它感到完全困惑项目,我在一些职位上用谷歌搜索,但找不到与我现有项目可行的任何内容。

以下是EF和存储库结构以及DI(Microsoft Unity)。

实体:

public class GenericDo 
        {
            public DateTime CreatedDate {get;set;}
            public string CreatedBy {get;set;}
        }
    public class UsersDo : GenericDo
        {
            public int UserId {get;set;}
            public string Username {get;set;}
            ....
        }
    public class UserProfileDo : GenericDo
        {
            public int Id {get;set}
            public int UserId {get;set;}
            public string Address {get;set;}
            ....
        }

接口:

public interface IGenericDao : IGenericDao<GenericDo> {}
    public interface IGenericDao<T> 
    {
        void Add(T entity);
        T Get(object Id);
        ....
    }
public interface IUsersDao : IUsersDao<UsersDo> {}
public interface IUserProfileDao : IUserProfileDao<UserProfileDo>{}

接口实现:

public class GenericDao<T> : IGenericDao<T> where T : class
        {
            private readonly DataContext context;
            public GenericDao(DataContext _context)
            {
                this.context = _context;
            }
            public void Add(T entity)
            {
                context.Set<T>().Add(entity);
            }
            public T Get(object Id)
            {
                return context.Set<T>().Find(Id);
            }
        }
    public class UsersDao : GenericDao<UsersDo>, IUsersDao
        {
            public UsersDao(DataContext context) : base (context){}
        }
    public class UserPorfileDao : GenericDao<UserProfileDo>, IUserProfileDao
        {
            public UserPorfileDao(DataContext context) : base (context){}
        }

Global.asax中的依赖项注入设置。

var container = this.AddUnity();
    container.RegisterType<IUsersDao, UsersDao>();
    container.RegisterType<IUserProfileDao, UserProfileDao>();

现在在我的主要网页(ASP.Net)中

public partial class Default : System.Web.UI.Page
    {
        private readonly IUsersDao usersDao;
        private readonly IUserProfileDao userProfileDao;
        public Default(IUsersDao _userDao, IUserProfileDao _userProfileDao)
        {
            this.usersDao = _userDao;
            this.userProfileDao = _userProfileDao;
        }
        // Now for testing purpose, i update record.
        protected void Page_Load(object sender, EventArgs e)
        {
            UsersDo user = usersDao.Get(1);
            user.Username = "new system";

            UserProfileDo userProfile = userProfileDao.Get(1);
            userProfile.Address = "new address";

            // Now here i am confused about setting up common Save method to update database with transaction.
        }
    }

EntityFramework的DbContext已经实现了工作单元,因此不必添加另一抽象层来实现这一点。

甚至可能怀疑如果使用实体框架,创建Repository模式是否真的有用。 除了使用分层架构和存储库,您还可以调查使用切片更多的架构并直接使用DbContext是否更好。

此外,拥有仅将调用委派给实体框架DbContext的“通用Dao”有什么好处? 它只是抽象的另一个层次,它增加了额外的复杂性,但却没有给您带来任何附加价值。

工作单元将数据库操作封装在一个对象中,并对其进行跟踪。 在实体框架中, DbContext实现此行为,而DbSet<>实现存储库。 人们创建自己的包装器的原因是,如果需要,可以将实体框架交换到另一个ORM,或者可以模拟实体框架进行测试。

UnitOfWork模式与实体框架一起使用。

存储库和工作单元模式旨在在应用程序的数据访问层和业务逻辑层之间创建一个抽象层。 实施这些模式可以帮助您的应用程序与数据存储区中的更改隔离开来,并可以促进自动化的单元测试或测试驱动的开发(TDD)。

第一步是创建存储库。 存储库是一个将方法公开给业务层的类

第二步:您可以如下所示创建UnitOfWork实现。 有对应于每个存储库的属性。 然后,在业务层中注入工作单元以使用存储库方法。

public class UnitOfWork : IDisposable
{
    private SchoolContext context = new SchoolContext();
    private GenericRepository<Department> departmentRepository;
    private GenericRepository<Course> courseRepository;

    public GenericRepository<Department> DepartmentRepository
    {
        get
        {

            if (this.departmentRepository == null)
            {
                this.departmentRepository = new GenericRepository<Department>(context);
            }
            return departmentRepository;
        }
    }

}

请参考以下文档: https : //docs.microsoft.com/zh-cn/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-和单元的功图案-在-AN-ASP-净-MVC的应用程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM