本文介绍了工作模式实现单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的ASP.NET MVC和Entity Framework code第一个应用程序。我使用的存储库,并与影响工作模式的单位从下面的链接。

I am creating an application with ASP.NET MVC and Entity framework code first. I am using repository and unit of work pattern with influence of from following link.

http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

在这里,我对单位的实施工作,该链接工作单元通过的问题在课堂上直接写入实体本身喜欢的是落实。

Here I have question about the implementation of Unit Of Work in that link unit of work is implemented via directly writing entities in class itself like.

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

    public GenericRepository<Department> DepartmentRepository
    {
        get
        {

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

}

你认为执行是不够好,因为每次我添加/删除的时间,我需要改变我的工作类的单位实体。我认为,工作组不应该依赖于实体。因为在基于客户反馈我的应用程序,我们要经常添加/删除实体。

Do you think that implementation is good enough because every time I add/remove entities I need to change my Unit of work class. I believe that Unit of work should not be dependent on entities. Because in my application based on Client feedback we are going to frequently add/remove entities.

我可能听起来愚蠢的,但让我知道这对你的看法。

I may sound stupid but let me know your views on that.

推荐答案

工作模式的单位是在实体框架已经实施。

The Unit of Work pattern is already implemented in Entity Framework.

本的DbContext是你的工作单位。
每个IDbSet是一个资源库。

The DbContext is your Unit of Work.Each IDbSet is a Repository.

using (var context = new SchoolContext())   // instantiate our Unit of Work
{
    var department = context.Departments.Find(id);
}

这篇关于工作模式实现单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:40