Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
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);
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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