Question

I am working on a C# MVC5 project using EF6. Currently the context is created in the UnityConfig.cs class, which is called from the Application_Start() in Global. See some of use UnityConfig class below:

public static void RegisterComponents()
{
   var container = new UnityContainer();
   BlogSite.Domain.BlogSiteModelContainer context = new Domain.BlogSiteModelContainer();
   container.RegisterType<IBlogRepository, BlogRepository>(new InjectionConstructor(context));
}

The problem with this is that the database may be updated directly and so the EF context will become inconsistent. So I need to change it so that it is re-loaded each time there is a new http request.

I have found that PerRequestLifetimeManager may be the solution for this, however, I can't find a good example of how I should use this.

How should I go about implementing this? Or is there a better way?


So I have tried a few things but still not getting anywhere. I realise I can add in a lifetime manager with the container.RegisterType() method, but I assume that only applies to the repository and not the Entity Framework context.

e.g.

container.RegisterType<IBlogRepository, BlogRepository>(new PerRequestLifetimeManager(), new InjectionConstructor(context));

I'm still not sure how set the lifetime of the context. I realise I'll probably have to re-format the code a bit but I can't find an example of this situation anywhere.


I've been doing more research, and have looked at the idea of creating a Unit of Work to create the context within and pass it to the repositories. Not sure if its the best way forward, but it seems to be one that might work. Any thoughts on that?

Was it helpful?

Solution

Found the solution. I needed to create a UnitOfWork class which I created the context within and the repositories. I then used Unity to inject the UnitOfWork class into the controllers with the PerRequestLifetimeManager lifetime manager.

So my UnitOfWork class (which is stored in my DAL project) is:

public class UnitOfWork : IDisposable
{
    private static BlogSite.Domain.BlogSiteModelContainer context;
    public IBlogRepository blogRepository;

    public UnitOfWork()
    {
        context = new BlogSite.Domain.BlogSiteModelContainer();;
        blogRepository = new BlogRepository(context);
    }

    public void Save()
    {
        context.SaveChanges();
    }

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

And I register this simply in my UnityConfig class with the following:

container.RegisterInstance<UnitOfWork>(new UnitOfWork(), new PerRequestLifetimeManager());

In my controllers I then just had a few small changes to make to the constructors so that they would accept the UnitOfWork rather than the repositories.

E.g.

IBlogRepository blogRepo;

public BlogController(UnitOfWork unitOfWork)
{
   blogRepo = unitOfWork.blogRepository;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top