Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle DBContext when using Ninject

Tags:

I am trying to use Ninject and OpenAccess for the first time. Please help me with the following. Here is what my project looks like...

public class ContentController : Controller {     private ContentService contentSvc;      public ContentController(ContentService contentSvc)     {         this.contentSvc = contentSvc;     } } 

The following class is under a folder in my web app.

public class ContentService {     private IContentRepository contentRepository;      public ContentService(IContentRepository contentRepository)     {         this.contentRepository = contentRepository;     }      public void InsertContent(Content content)     {          contentRepository.InsertContent(content);     } } 

The following repository belongs to a separate assembly.

public class ContentRepository : IContentRepository {     DBContext db;     public ContentRepository(DBContext _db)     {         db = _db;     }      public void InsertContent(Content content)     {              db.Add(content);     } }    

Here is what Ninject binding look like..

kernel.Bind<ContentService>().To<ContentService>().InRequestScope(); kernel.Bind<IContentRepository>().To<ContentRepository>().InRequestScope().WithConstructorArgument("_db", new DBContext()); 

Everything works fine if I fetch one page at a time. I am using a simple tool 'XENU' to fetch multiple pages simultaneously. This is when I get errors with DBContext by fetching multiple pages at a time.

I am not sure if Ninject is dosposing the DBContext in each REQUEST?? I get different errors, e.g. 'Object reference not set to an instance of an object.', OR 'ExecuteReader requires an open and available Connection. The connection's current state is open.'

P.S.

I have ContentService under a folder in my MVC web app. ContentRepository is a separate assembly. I will be adding business logic in ContentService and use 'ContentRepository' only for CRUD operations. Also, please let me know if this architecture is okay or is there a better way to create services and repositories.

like image 274
eadam Avatar asked Aug 12 '12 11:08

eadam


People also ask

Is DbContext scoped or Singleton?

First, DbContext is a lightweight object; it is designed to be used once per business transaction. Making your DbContext a Singleton and reusing it throughout the application can cause other problems, like concurrency and memory leak issues.

What is the use of DbContext in MVC?

A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.


1 Answers

Here's how I would do your Ninject bindings,

kernel.Bind<DBContext>().ToSelf().InRequestScope(); kernel.Bind<ContentService>().ToSelf().InRequestScope(); kernel.Bind<IContentRepository>().To<ContentRepository>().InRequestScope(); 

This pattern should work fine in the example above with EF and Ninject.

like image 72
Not loved Avatar answered Oct 26 '22 11:10

Not loved