简体   繁体   English

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

[英]UnitOfWork with Repository and Entity Framework

I am working on a project with Entity Framework where i have implemented Repository pattern and DI (Microsoft Unity), now to maintain the database transactions i want to implement the UnitOfWork pattern, but i am totally confused about how shall i implement it in my current project, i google around few posts, but could not find anything doable with my existing project. 我正在使用Entity Framework进行项目,其中我已经实现了存储库模式和DI(Microsoft Unity),现在要维护数据库事务,我想实现UnitOfWork模式,但是我对如何在当前版本中实现它感到完全困惑项目,我在一些职位上用谷歌搜索,但找不到与我现有项目可行的任何内容。

Below is the EF and Repository structure along with DI (Microsoft Unity). 以下是EF和存储库结构以及DI(Microsoft Unity)。

Entities: 实体:

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;}
            ....
        }

Interface: 接口:

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>{}

Interface Implementation: 接口实现:

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){}
        }

Dependency Injection Setup in Global.asax. Global.asax中的依赖项注入设置。

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

Now in my main webpage(ASP.Net) 现在在我的主要网页(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's DbContext already implements Unit of Work, so it is not necessary to add yet another layer of abstraction to implement this. EntityFramework的DbContext已经实现了工作单元,因此不必添加另一抽象层来实现这一点。

One could even doubt if creating a Repository pattern is actually helpful if you're using Entity Framework. 甚至可能怀疑如果使用实体框架,创建Repository模式是否真的有用。 Instead of using a layered architecture and using a Repository, you could investigate whether it is not better to use a more sliced architecture and use the DbContext directly. 除了使用分层架构和存储库,您还可以调查使用切片更多的架构并直接使用DbContext是否更好。

Also, what is the benefit of having a 'Generic Dao' that just delegates calls to Entity Frameworks DbContext ? 此外,拥有仅将调用委派给实体框架DbContext的“通用Dao”有什么好处? It's just yet another level of abstraction which adds extra complexity but doesn't give you any added value. 它只是抽象的另一个层次,它增加了额外的复杂性,但却没有给您带来任何附加价值。

Unit of work ecapsulates the database operations in a single object and keeps track of them. 工作单元将数据库操作封装在一个对象中,并对其进行跟踪。 In Entity Framework DbContext implements this behaviour and DbSet<> implements the repository. 在实体框架中, DbContext实现此行为,而DbSet<>实现存储库。 The reason why people create their own wrappers around is to be able to swap Entity Framework for another ORM, if needed or to mock Entity Framework for testing,. 人们创建自己的包装器的原因是,如果需要,可以将实体框架交换到另一个ORM,或者可以模拟实体框架进行测试。

UnitOfWork pattern is used with Entity Framework. UnitOfWork模式与实体框架一起使用。

The repository and unit of work patterns are intended to create an abstraction layer between the data access layer and the business logic layer of an application. 存储库和工作单元模式旨在在应用程序的数据访问层和业务逻辑层之间创建一个抽象层。 Implementing these patterns can help insulate your application from changes in the data store and can facilitate automated unit testing or test-driven development (TDD). 实施这些模式可以帮助您的应用程序与数据存储区中的更改隔离开来,并可以促进自动化的单元测试或测试驱动的开发(TDD)。

First step is to create repositories. 第一步是创建存储库。 Repository is a class which exposes methods to business layer 存储库是一个将方法公开给业务层的类

Second step: You can create UnitOfWork implementation as shown below. 第二步:您可以如下所示创建UnitOfWork实现。 There are properties corresponding to every repository. 有对应于每个存储库的属性。 Then you inject Unit of Work in your business layer to use the repository methods. 然后,在业务层中注入工作单元以使用存储库方法。

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;
        }
    }

} }

refer documentation at: https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application 请参考以下文档: 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