Click here to Skip to main content
15,881,882 members

entity and repository pattern with ninject, Dispose Issue

Member 8357920 asked:

Open original thread
I have build my site using entity and repository pattern with ninject injection. My problem is my connections don't seem to get disposed. I have around 30 repositories (one for each table) and I get sql expiration timout preety quick. I can't use the regular using statement because the code recognize only the interface before the injection. (in each controler I have my repositories interface instances which get injected via ninject).

I have searched the net but couldn't find a solution that was accurate for me. can anyone please help me? code example:

this is in the ninject controller under addBindings():

C#
ninjectKernel.Bind<IMovieRepository>().To<MovieRepository>().InRequestScope();


and one of my repositories:

C#
public class MovieRepository : IMovieRepository, IDisposable 
        {
         private Entities dataContext = new Entities();
         public System.Data.Entity.DbContext DbContext 
         {
            get { return dataContext ?? (dataContext = new Entities()); } 
         }
         public void Dispose() { dataContext.Dispose(); }
        }


and in the Global.asax file:

C#
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory() as IControllerFactory);
Tags: C#, Entity Framework, Repository

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900