0

In Asp.Net MVC, the default template comes with Startup class in Startup.Auth.cs

public partial class Startup { // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 public void ConfigureAuth(IAppBuilder app) { // Configure the db context, user manager and signin manager to use a single instance per request app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); 

According to the comment in this template, an instance of ApplicationDbContext is created per request.

In my application, I am using the Repository Pattern and inject ApplicationDbContext into the repositories. In my case I am using Ninject, which can share the same context among multiple repositories if needed.

Is there any way to use the same injected ApplicationDbContext in Startup.Auth? Not really sure if this is a good idea to change the default template, at the same time we are creating 2 instances of the same context in 1 request...

In my Ninject code, I am binding ApplicationDbContext to itself as below:

private static void RegisterServices(IKernel kernel) { kernel.Bind<ApplicationDbContext>().ToSelf().InRequestScope(); 

Not really sure if this a bad practice to bind the context to itself instead of creating an interface? (though it is working fine).

8
  • 2
    In my project I get rid of the Owin creation and I delegate all creation of the ApplicationDbContext, ApplicationUserManager and ApplicationSignInManager to my DI container. Don't mix such things otherwise you'lll end up having akward bugs Commented May 2, 2018 at 20:21
  • @CodeNotFound: thanks, are you able to share how this can be done? Commented May 2, 2018 at 21:59
  • 1
    Why inject into Startup.Auth when you can just call ApplicationDbContext.Create() to get an instance. Just don't forget to dispose of it when done. Just because you are using DI, does not mean you should inject stuff into every (im)possible place. Commented May 3, 2018 at 10:58
  • @trailmax: thanks for clarifying this, so your recommendation is that 2 instances of ApplicationDbContext is fine? I don't like the idea of changing the built in template, I just thought since ninject can share the DB context, it might be good idea, and was looking for some input on this. Commented May 3, 2018 at 22:25
  • 1
    @Hooman ConfigureAuth is executed on the application startup, possibly even before you have your Ninject container initialised. And it will go out of scope before you'll have any http requests, so no chance to have clashing dbContext instances. Commented May 3, 2018 at 22:31

1 Answer 1

1

Not really sure if this a bad practice to bind the context to itself instead of creating an interface?

Registering DbContext as self and per-request lifetime is pretty normal.

I personally like to implement IDbContext interface, so that it is more cleaner to mock when I unit test the repositories. So, my repositories depend on abstraction instead of concrete implementation.

public class ApplicationDbContext : DbContext, IDbContext { } 

IDbContext

public interface IDbContext { DbSet<MyEntity> MyEntities { get; set; } ... } 
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Win, appreciate your answer. Any comment about Startup.Auth?
I just see that you have registered twice. If you use Ninject, do not register the dependenices inside Startup.ConfigureAuth. Instead, place them inside Ninject bindings, and let Ninject IoC container taking care of Register, Resolve, and Release.
Thanks, I am not sure how can I inject IApplicationDbContext into ConfigureAuth(IAppBuilder app)?
Could you elaborate more on what you are trying to achieve, or create a new question with sample code?
in MVC, there ts a Startup class, which calls: ConfigureAuth(app); If I want to Inject ApplicationDbContext, I need to inject it into ConfigureAuth and I don't know how to do this.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.