Put a constructor that accepts DbContext on the deriving class of your IRepositoryFactory
class RepositoryFactory : IRepositoryFactory
{
DbContext _dbc;
public RepositoryFactory(DbContext db)
{
_dbc = db;
}
public IRepository CreateRepository()
{
return new Repository(_dbc);
}
}
Then you bind that DbContext on your Ninject injection bindings, put this along with your other bindings:
ninjectKernel.Bind<DbContext>().To<EfDbContext>().InRequestScope();
I'm just taking a guess here, this is how your injection bindings for RepositoryFactory looks like:
ninjectKernel.Bind<IRepositoryFactory<Person>>().To<RepositoryFactory<Person>>();
If you made those two bindings next to each other (order is not important), Ninject shall be able to do an injection of value to the DbContext parameter of your RepositoryFactory's constructor.
An example
Look at line 60 to 76 here: http://code.google.com/p/flexigrid-crud-example/source/browse/trunk/FlexigridCrudDemo/FlexigridCrudDemo/NinjectDependencyResolver.cs
See the repository pattern for EF here, line 22: http://code.google.com/p/to-the-efnh-x/source/browse/trunk/ToTheEfnhX/Ienablemuch.ToTheEfnhX.EntityFramework/EfRepository.cs
See the repository pattern for NHibernate here, line 24: http://code.google.com/p/to-the-efnh-x/source/browse/trunk/ToTheEfnhX/Ienablemuch.ToTheEfnhX.NHibernate/NhRepository.cs
And how I abstracted those two dissimilar ORMs with repository pattern, the depency injection for connections(DbContext for Entity Framework, Session for NHibernate) is facilitated by Ninject:
int target = 1; // memory, nhibernate, entity framework
switch (target)
{
case 0:
ninjectKernel.Bind<IRepository<Person>>().ToMethod(x =>
{
var person = new MemoryRepository<Person>();
person.Save(new Person { Username = "Hello", Firstname = "Yo", FavoriteNumber = 9 }, null);
person.Save(new Person { Username= "See", Firstname = "Nice" }, null);
return person;
}
).InSingletonScope();
break;
case 1:
ninjectKernel.Bind<ISession>().ToMethod(x => ModelsMapper.GetSessionFactory().OpenSession()).InRequestScope();
ninjectKernel.Bind<Ienablemuch.ToTheEfnhX.IRepository<Person>>().To<Ienablemuch.ToTheEfnhX.NHibernate.NhRepository<Person>>();
ninjectKernel.Bind<Ienablemuch.ToTheEfnhX.IRepository<Country>>().To<Ienablemuch.ToTheEfnhX.NHibernate.NhRepository<Country>>();
break;
case 2:
ninjectKernel.Bind<DbContext>().To<EfDbContext>().InRequestScope();
ninjectKernel.Bind<Ienablemuch.ToTheEfnhX.IRepository<Person>>().To<Ienablemuch.ToTheEfnhX.EntityFramework.EfRepository<Person>>();
ninjectKernel.Bind<Ienablemuch.ToTheEfnhX.IRepository<Country>>().To<Ienablemuch.ToTheEfnhX.EntityFramework.EfRepository<Country>>();
break;
default:
break;
}