每個我的倉庫類是這樣的:如何注入我的數據庫上下文到我所有的倉庫類
public class ProfileRepository : IProfileRepository{
private MyEntities myEnt = new MyEntities();
...
}
我注入我的倉庫類,像這樣:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IProfileRepository>().To<ProfileRepository>();
....
GlobalHost.DependencyResolver = new NinjectDependencyResolver(kernel);
GlobalHost.DependencyResolver.Register(typeof(IConnectionIdGenerator),() => new MyConnectionFactory());
RouteTable.Routes.MapHubs(new NinjectDependencyResolver(kernel));
}
我要注入MyEntities上下文到我所有的存儲庫類中,所以我沒有在每個存儲庫類中都有它的實例。我該如何做到這一點?這會是一個更好的做法嗎?
換句話說,我不應該注入我的上下文到我的倉庫嗎? – anthonypliu
如果你有存儲庫,你*必須*注入你的上下文到它們中,並且你*必須*確保你每個請求只有一個上下文(實際上,這不是必須的,但它會讓你的生活變得更容易)。我說 - 根本沒有存儲庫,有更好的方法來完成單元測試。 – zmbq
@anthonypliu:你應該。通過不注入上下文,您無法在不同場景中正確控制上下文的生命週期。 –