Using Unity to create Entity Framework objects for the Unit of Work/Repository pattern

.net c# entity-framework repository-pattern unit-of-work

Question

I'm attempting to apply the Unit of Work/Repository design as stated in this article: http://blogs.msdn.com/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx

For example, an EF datacontext that has been updated with a partial class to add an IUnitOfWork interface is an example of an IUnitOfWork implementation that each Repository must accept. Actually, I'm using.net 3.5 rather than 4.0. This is how my fundamental Data Access constructor looks:

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

Good news thus far,

I want to use the Unity Framework to incorporate Dependency Injection.

It was a challenge to get the EF data context to be formed with Unity since it had difficulties resolving the constructor. In the end, I added a new, overloaded constructor to my partial class and tagged it with[InjectionConstructor] .

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

(I am aware that I must provide the connection string to the base object; but, I may wait until all the objects have initialized successfully.)

I can happily resolve my entity framework object into an IUnitOfWork instance using this method, as follows:

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

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

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

    // ...
}

Great. The DataAccessLayer simply needs to know the interface, so I presume I need to instantiate it as part of the Unity Resolve statement and feed it the relevant IUnitOfWork interface. Next, I need to build the reference to the repository object for the DataAccessLayer.

Previously, I would have just supplied the database connection string to the Repository constructor, and it would have disappeared. Instead, a local Entity Framework object was constructed and utilized only for the lifetime of the Repository function. This is different in that during the Unity Resolve statement, I build an Entity Framework instance as an IUnitOfWork implementation, and I need to provide that instance into the constructor of the Repository. Is that doable, and if so, how?

The issue of how to establish the repository with the IUnitOfWork object that the DAL is being resolved with wouldn't be resolved by making the repository a property and designating it as a dependency, however.

I'm not sure whether I really understand this concept, and I'll be happy to accept suggestions on how to implement it effectively. Entity Framework will remain, but Unity may be substituted if it turns out not to be the best course of action. Please let me know if I have anything sideways or upside down.

1
1
3/28/2010 4:09:48 PM

Popular Answer

Here is a revised version of the response: Reusing instances using the Unity framework

Use of a ContainerControlledLifetimeManager, sometimes known as a Singleton, is the answer.

1
5/23/2017 12:02:30 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow