创建具有统一实体框架对象工作单位/ Repository模式 [英] Creating Entity Framework objects with Unity for Unit of Work/Repository pattern

查看:117
本文介绍了创建具有统一实体框架对象工作单位/ Repository模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的工作/存储库单元模式,如下所述: <一href="http://blogs.msdn.com/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx" rel="nofollow">http://blogs.msdn.com/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx

I'm trying to implement the Unit of Work/Repository pattern, as described here: http://blogs.msdn.com/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx

这要求每个存储库来接受IUnitOfWork实现,例如DataContext的一个局部类扩展添加一个IUnitOfWork接口的EF。实际上,我使用.net 3.5,而不是4.0。我的基本数据访问的构造是这样的:

This requires each Repository to accept an IUnitOfWork implementation, eg an EF datacontext extended with a partial class to add an IUnitOfWork interface. I'm actually using .net 3.5, not 4.0. My basic Data Access constructor looks like this:

public DataAccessLayer(IUnitOfWork unitOfWork,
                       IRealtimeRepository realTimeRepository)
{
    this.unitOfWork = unitOfWork;
    this.realTimeRepository = realTimeRepository;
}

到目前为止,一切都很好。

So far, so good.

我想要做的就是使用统一框架添加依赖注入。

What I'm trying to do is add Dependency Injection using the Unity Framework.

获取EF数据上下文中使用Unity创建是一种冒险,因为它遇到了麻烦解决构造 - 我到底做的是建立在我的一个新的重载的构造部分类另一个构造,并标明与 [InjectionConstructor]

Getting the EF data context to be created with Unity was an adventure, as it had trouble resolving the constructor - what I did in the end was to create another constructor in my partial class with a new overloaded constructor, and marked that with [InjectionConstructor].

[InjectionConstructor]
public communergyEntities(string connectionString, string containerName)
    : this()
{
    // ...
}

(我知道我需要通过连接字符串中的基础对象,可以等到一次,我已经得到了所有正确的初始化对象)

因此​​,使用这种技术,我可以高兴地解决我的实体框架对象作为这样一个IUnitOfWork例如:

So, using this technique, I can happily resolve my entity framework object as an IUnitOfWork instance thus:

using (IUnityContainer container = new UnityContainer())
{
    container.RegisterType<IUnitOfWork, communergyEntities>();

    container.Configure<InjectedMembers>()
        .ConfigureInjectionFor<communergyEntities>
            (new InjectionConstructor("a", "b"))

    DataAccessLayer target = container.Resolve<DataAccessLayer>();

    // ...
}

大。我现在要做的是创建引用的DataAccessLayer储存库对象 - 在DAL只需要知道接口,所以我猜测,我需要实例它作为统一解决语句的一部分,它传递适当的IUnitOfWork接口。

Great. What I need to do now is create the reference to the repository object for the DataAccessLayer - the DAL only needs to know the interface, so I'm guessing that I need to instantiate it as part of the Unity Resolve statement, passing it the appropriate IUnitOfWork interface.

在过去,我会刚刚通过了库构造数据库连接字符串,它会消失,创造了当地的实体框架对象,并使用了公正的库法的寿命。这是不同的,在我创建一个实体框架实例作为统一解决语句期间IUnitOfWork实施,那就是比如我需要传递到存储库的构造 - 是可能的,如果是这样,怎么样?

In the past, I would have just passed the Repository constructor the db connection string, and it would have gone away, created a local Entity Framework object and used that just for the lifetime of the Repository method. This is different, in that I create an Entity Framework instance as an IUnitOfWork implementation during the Unity Resolve statement, and it's that instance I need to pass into the constructor of the Repository - is that possible, and if so, how?

我想知道如果我可以让存储库的属性,并将其标记为依赖,但仍然不能解决如何创建与IUnitOfWork对象存储库中的问题,即DAL正在解决与

I'm wondering if I could make the Repository a property and mark it as a Dependency, but that still wouldn't solve the problem of how to create the Repository with the IUnitOfWork object that the DAL is being Resolved with

我不知道如果我理解正确的这一模式,并会乐意接受建议最好的方式来实现它 - 实体框架是住,但统一可换出,如果不是最好的方法。如果我有整个事情倒过来,请告诉我

I'm not sure if I've understood this pattern correctly, and will happily take advice on the best way to implement it - Entity Framework is staying, but Unity can be swapped out if not the best approach. If I've got the whole thing upside down, please tell me

推荐答案

这是改写,并回答了在这里:<一href="http://stackoverflow.com/questions/2412563/unity-framework-reusing-instance">http://stackoverflow.com/questions/2412563/unity-framework-reusing-instance

This was rephrased and answered here: http://stackoverflow.com/questions/2412563/unity-framework-reusing-instance

该解决方案是使用一个ContainerControlledLifetimeManager - 又名辛格尔顿: http://msdn.microsoft.com/en-us/library/dd203242.aspx

The solution is to use a ContainerControlledLifetimeManager - aka Singleton: http://msdn.microsoft.com/en-us/library/dd203242.aspx

感谢杰夫

这篇关于创建具有统一实体框架对象工作单位/ Repository模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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