开发者

Creating Entity Framework objects with Unity for Unit of Work/Repository pattern

开发者 https://www.devze.com 2022-12-22 03:27 出处:网络
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-fr开发者_如何学运

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-fr开发者_如何学运维amework-4-0.aspx

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.

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()
{
    // ...
}

(I know I need to pass the connection string to the base object, that can wait until once I've got all the objects initialising correctly)

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>();

    // ...
}

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.

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?

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


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消