Bind ObjectContext using Ninject in InSingletonScope or not
-
20-06-2021 - |
题
I have repository with constructor which looks like
public Repository(ObjectContext context)
{
_context = context;
_objectSet = _context.CreateObjectSet<T>();
}
I use Ninject in my MVC app to bind ObjectContext like this
var connectionString = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString;
kernel.Bind(typeof(ObjectContext)).ToMethod(context => new Entities(connectionString)).InSingletonScope();
I am not sure about InSingletonScope
in this case. Should I use it or not?
解决方案
Almost certainly not, you want to make sure that each repository is separate and for example a call to savechanges on one will not commit things from another repository.
I would also recommend using DbContext instead of ObjectContext.
不隶属于 StackOverflow