4

I'm running into issues with multiple contexts and the main solution that comes up is to share the context between repositories however I haven't found a good example on how to do this.

For reference, I'm using an MVC Web App to connect to my data layer. I'd like to have one context per user request (assuming this is correct).

Thanks,

Edit -

This is my solution with the help of BrokenGlass's links and the following SO Question:

I essentially implemented the Unit Of Work pattern and Dependency Injection. I should have mentioned in addition to using MVC, I'm also using Ninject.

In a given repository constructor (see the link below for Unit Of Work pattern details):

public class PersonRepository : IPersonRepository
{
    private readonly MyContext _context;

    public PersonRepository(IUnitOfWork unitOfWork) 
    {
        if (unitOfWork == null)
            throw new ArgumentNullException("unitOfWork");

        _context = unitOfWork as MyContext;
    } 

    //...
}

In my MVC App in the NinjectMVC3 class (the key being the InRequestScope() method):

private static void RegisterServices(IKernel kernel)
{
     kernel.Bind<IUnitOfWork>().To<MyContext>().InRequestScope();
     kernel.Bind<IPersonRepository>().To<PersonRepository>();
     //...
}  
Community
  • 1
  • 1
matto0
  • 1,055
  • 2
  • 12
  • 22

2 Answers2

4

Your repository layer should provide a unit of work that represents a single request and uses a context object that is then used on all individual repositories that are needed to fulfill the request.

For HTTP / web apps specifically you can cache the db context in the HttpContext.Current.Items which stores shared data for each HTTP request. Also check out this similar SO thread for details: attaching linq to sql datacontext to httpcontext in business layer

Community
  • 1
  • 1
BrokenGlass
  • 149,257
  • 27
  • 271
  • 318
  • Thanks for the Unit of Work link, I wasn't implementing any type of DI with in my repositories and essentially that's what I needed to do. I'm also using Ninject so it complicated the answer a bit, but ended up making the solution easier. I'll update my question with my final solution. – matto0 Jun 19 '11 at 22:27
0

My answer to C#/EF and the Repository Pattern: Where to put the ObjectContext in a solution with multiple repositories? provides an implementation of a RepositoryProvider that works with a single instance of an ObjectContext. You could use the code the same way, with a LINQ-to-SQL DataContext instead of an EF ObjectContext. The benefit of the RepositoryProvider over Ninject, is that the RepositoryProvider implementation is not bound to a specific DI framework, and can itself be configured in any DI framework.

In addition, you can manage creation and scoping of the DataContext to either a Thread or a WebRequest (such as in @BrokenGlass's answer with HttpContext.Current.Items) by using the DataContextFactory class from here.

Community
  • 1
  • 1
smartcaveman
  • 38,142
  • 26
  • 119
  • 203