使用Unity for Work / Repository模式创建实体框架对象 [英] Creating Entity Framework objects with Unity for Unit of Work/Repository pattern

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

问题描述

我正在尝试实现工作单位/仓库模式,如下所述:
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实现,例如使用部分类扩展的EF数据文本添加IUnitOfWork接口。我实际上是使用.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;
}

到目前为止,这么好。

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

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

使用Unity创建EF数据上下文是一个冒险,因为解决构造函数时遇到麻烦 - 我最后所做的是在一个新的重载构造函数中在我的部分类中创建另一个构造函数,并用 [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只需要知道该接口,所以我猜测我需要将其作为Unity Resolve语句的一部分进行实例化,将其传递给适当的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.

在过去,我刚刚通过Repository构造函数的db连接字符串,它将会消失,创建了一个本地的实体框架对象并使用这就是Repository方法的一生。这是不同的,因为我在Unity Resolve语句中创建了一个实体框架实例作为IUnitOfWork实现,而那个实例我需要传入Repository的构造函数 - 是否可能,如果是这样的话?

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?

我想知道如果我可以使Repository成为一个属性,并将其标记为依赖关系,但是仍然无法解决如何创建Repository的问题DAL正在解决的IUnitOfWork对象使用

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

我不知道我是否正确地理解了这种模式,并且将乐意采取最佳实施方式的建议它 - 实体框架正在停留,但如果不是最好的方法,Unity可以被换出。如果我把整个事情颠倒过来,请告诉我

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

推荐答案

这是改写并在这里回答:无效框架 - 重用实例

This was rephrased and answered here: Unity framework - reusing instance

解决方案是使用ContainerControlledLifetimeManager - aka Singleton:
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

这篇关于使用Unity for Work / Repository模式创建实体框架对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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