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

Posted by TobyEvans on Stack Overflow See other posts from Stack Overflow or by TobyEvans
Published on 2010-03-09T08:46:49Z Indexed on 2010/03/09 8:51 UTC
Read the original article Hit count: 1260

Hi there,

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

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

thanks

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about unitofwork