title: Ninject Inject Common DbContext Into Numerous Repositories
body: <p>There’s something which I am doing that is working, but I think it can probably be done a lot better (and therefore, with more maintainability).</p>
  
  <p>I am using Ninject to inject various things into a controller. The problem which I needed to solve is that the DbContext for each repository needed to be the same. That is, the same object in memory.</p>
  
  <p>Whilst, the following code does achieve that, my Ninject <em>common config file</em> has started to get quite messy as I have to write similar code for each controller:</p>
  
  <pre><code>kernel.Bind&lt;OrderController&gt;().ToMethod(ctx =&gt;
  {
      var sharedContext = ctx.Kernel.Get&lt;TTSWebinarsContext&gt;();
      var userAccountService = kernel.Get&lt;UserAccountService&gt;();
      ILogger logger = new Log4NetLogger(typeof(Nml.OrderController));
      ILogger loggerForOrderManagementService = new Log4NetLogger(typeof(OrderManagementService));
  
  
      var orderManagementService = new OrderManagementService(
          new AffiliateRepository(sharedContext),
          new RegTypeRepository(sharedContext),
          new OrderRepository(sharedContext),
          new RefDataRepository(),
          new WebUserRepository(sharedContext),
          new WebinarRepository(sharedContext),
          loggerForOrderManagementService,
          ttsConfig
          );
  
      var membershipService = new MembershipService(
          new InstitutionRepository(sharedContext),
          new RefDataRepository(),
          new SamAuthenticationService(userAccountService),
          userAccountService,
          new WebUserRepository(sharedContext)
          );
  
      return new OrderController(membershipService, orderManagementService, kernel.Get&lt;IStateService&gt;(), logger);
  }).InRequestScope();    
  </code></pre>
  
  <p>Is there a neater way of doing this?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Tried the following code. As soon as I make a second request, an exception is chucked that the DbContext has already been disposed. </p>
  
  <pre><code>kernel.Bind&lt;TTSWebinarsContext&gt;().ToSelf().InRequestScope();
  
  string baseUrl = HttpRuntime.AppDomainAppPath;
  kernel.Bind&lt;IStateService&gt;().To&lt;StateService&gt;().InRequestScope();
  kernel.Bind&lt;IRefDataRepository&gt;().To&lt;RefDataRepository&gt;().InRequestScope().WithConstructorArgument("context", kernel.Get&lt;TTSWebinarsContext&gt;()); 
  
  var config = MembershipRebootConfig.Create(baseUrl, kernel.Get&lt;IStateService&gt;(), kernel.Get&lt;IRefDataRepository&gt;());
  var ttsConfig = TtsConfig.Create(baseUrl);
  
  kernel.Bind&lt;MembershipRebootConfiguration&gt;().ToConstant(config);
  kernel.Bind&lt;TtsConfiguration&gt;().ToConstant(ttsConfig);
  
  kernel.Bind&lt;IAffiliateRepository&gt;().To&lt;AffiliateRepository&gt;().InRequestScope().WithConstructorArgument("context", kernel.Get&lt;TTSWebinarsContext&gt;());
  kernel.Bind&lt;IWebinarRepository&gt;().To&lt;WebinarRepository&gt;().InRequestScope().WithConstructorArgument("context", kernel.Get&lt;TTSWebinarsContext&gt;());
  kernel.Bind&lt;IWebUserRepository&gt;().To&lt;WebUserRepository&gt;().InRequestScope().WithConstructorArgument("context", kernel.Get&lt;TTSWebinarsContext&gt;());
  kernel.Bind&lt;IOrderRepository&gt;().To&lt;OrderRepository&gt;().InRequestScope().WithConstructorArgument("context", kernel.Get&lt;TTSWebinarsContext&gt;());
  kernel.Bind&lt;IInstitutionRepository&gt;().To&lt;InstitutionRepository&gt;().WithConstructorArgument("context", kernel.Get&lt;TTSWebinarsContext&gt;());
  kernel.Bind&lt;IUserAccountRepository&gt;().To&lt;DefaultUserAccountRepository&gt;().InRequestScope();
  kernel.Bind&lt;IRegTypeRepository&gt;().To&lt;RegTypeRepository&gt;().InRequestScope().WithConstructorArgument("context", kernel.Get&lt;TTSWebinarsContext&gt;());
  
  kernel.Bind&lt;UserAccountService&gt;().ToMethod(ctx =&gt;
  {
      var userAccountService = new UserAccountService(config, ctx.Kernel.Get&lt;IUserAccountRepository&gt;());
      return userAccountService;
  });
  
  kernel.Bind&lt;IOrderManagementService&gt;().To&lt;OrderManagementService&gt;().InRequestScope();
  
  //RegisterControllers(kernel, ttsConfig);
  
  kernel.Bind&lt;AuthenticationService&gt;().To&lt;SamAuthenticationService&gt;().InRequestScope();
  kernel.Bind&lt;IMembershipService&gt;().To&lt;MembershipService&gt;().InRequestScope();
  </code></pre>
  
  <p>There's something about InRequestScope I'm misunderstanding.</p>