实体框架4是“工作单位”模拟通用存储库的方式? [英] Is the Entity Framework 4 "Unit of Work" pattern the way to go for generic repositories?

查看:118
本文介绍了实体框架4是“工作单位”模拟通用存储库的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究为我正在开发的一个新的ASP.NET MVC项目创建一个实体框架4通用存储库。我一直在看各种教程,他们似乎都使用工作单位模式...



根据我一直在阅读的内容,EF在ObjectContext,而您只是将其扩展为自己的工作单元。



资料来源: http://dotnet.dzone.com/news/using-unit-work-pattern-entity?utm_source=feedburner& ; utm_medium = feed& utm_campaign = Feed%3A + zones%2Fdotnet +(。NET + Zone)



为什么要这样做?
这是使用通用存储库的首选方式吗?



非常感谢,
Kohan。

解决方案

这不是我使用通用存储库的方式。首先,我将在当前请求中在ClassARepository,CalssBRepository和其他存储库之间共享ObjectContext。推荐使用IOC容器,使用注入和每个请求行为:



这是我的通用存储库的外观:

  public interface IRepository< T> 
{
//检索表
中的项目列表IQueryable< T>名单();
IQueryable< T> List(params string [] include);
//从独立项目创建
void Create(T item);
void Delete(int id);
T Get(int id);
T Get(int id,params string [] includes);
void SaveChanges();
}

public class Repository< T> :IRepository< T>其中T:EntityObject
{
private ObjectContext _ctx;

public Repository(ObjectContext ctx)
{
_ctx = ctx;
}


私有静态字符串EntitySetName
{
获取
{
返回String.Format(@{0}设置,typeof(T).Name);
}
}

private ObjectQuery< T> ObjectQueryList()
{
var list = _ctx.CreateQuery< T>(EntitySetName);
返回列表;
}

#region IRepository< T>成员

public IQueryable< T> List()
{
return ObjectQueryList()。OrderBy(@it.ID)。AsQueryable();
}

public IQueryable< T> List(params string [] include)
{
var list = ObjectQueryList();

foreach(string include in includes)
{
list = list.Include(include);
}

返回列表;
}

public void Create(T item)
{
_ctx.AddObject(EntitySetName,item);
}

public void Delete(int id)
{
var item = Get(id);
_ctx.DeleteObject(item);
}

public T Get(int id)
{
var list = ObjectQueryList();
return list.Where(ID = @ 0,id).First();
}

public T Get(int id,params string [] includes)
{
var list = List(includes);
return list.Where(ID = @ 0,id).First();
}

public void SaveChanges()
{
_ctx.SaveChanges();
}

#endregion

}

ObjectContext是通过构造函数注入的。 List()方法返回IQueryable,用于业务层(服务)对象中的进一步处理。服务层返回List或IEnumerable,所以在视图中没有延迟执行。



此代码是使用EF1创建的。 EF4版本可以有所不同,简单。


I am looking into creating an Entity Framework 4 generic repository for a new ASP.NET MVC project i am working on. I have been looking at various tutorials and they all seem to use the Unit of Work pattern ...

From what i have been reading, EF is using this already within the ObjectContext and you are simply extending this to make your own Units of Work.

Source: http://dotnet.dzone.com/news/using-unit-work-pattern-entity?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zones%2Fdotnet+(.NET+Zone)

Why would one go to the effort of doing this? Is this the preferred way of working with generic repositories?

Many thanks, Kohan.

解决方案

This is not the way I would work with generic repositories. First of all, I would share ObjectContext between ClassARepository, CalssBRepository and other repositories in current request. Using IOC container, using injection and per request behavior is recommended:

This is how my generic repositories look like:

public interface IRepository<T>
{
    //Retrieves list of items in table
    IQueryable<T> List();
    IQueryable<T> List(params string[] includes);
    //Creates from detached item
    void Create(T item);
    void Delete(int id);
    T Get(int id);
    T Get(int id, params string[] includes);
    void SaveChanges();
}

public class Repository<T> : IRepository<T> where T : EntityObject
{
    private ObjectContext _ctx;

    public Repository(ObjectContext ctx)
    {
        _ctx = ctx;
    }


    private static string EntitySetName
    {
        get
        {
            return String.Format(@"{0}Set", typeof(T).Name);
        }
    }

    private ObjectQuery<T> ObjectQueryList()
    {
        var list = _ctx.CreateQuery<T>(EntitySetName);
        return list;
    }

    #region IRepository<T> Members

    public IQueryable<T> List()
    {
        return ObjectQueryList().OrderBy(@"it.ID").AsQueryable();
    }

    public IQueryable<T> List(params string[] includes)
    {
        var list = ObjectQueryList();

        foreach(string include in includes)
        {
            list = list.Include(include);
        }

        return list;
    }

    public void Create(T item)
    {
        _ctx.AddObject(EntitySetName, item);
    }

    public void Delete(int id)
    {
        var item = Get(id);
        _ctx.DeleteObject(item);
    }

    public T Get(int id)
    {
        var list = ObjectQueryList();
        return list.Where("ID = @0", id).First();
    }

    public T Get(int id, params string[] includes)
    {
        var list = List(includes);
        return list.Where("ID = @0", id).First();
    }

    public void SaveChanges()
    {
        _ctx.SaveChanges();
    }

    #endregion

}

ObjectContext is injected through constructor. List() methods return IQueryable for further processing in business layer (service) objects. Service layer returns List or IEnumerable, so there is no deferred execution in views.

This code was created using EF1. EF4 version can be a little different and simpler.

这篇关于实体框架4是“工作单位”模拟通用存储库的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆