[英]Ninject and DbContext
Building a app with EF 6 and Ninject 3.2.2 I'm having some trouble wrapping my head around how to access the DbContext in a intelligent way. 使用EF 6和Ninject 3.2.2构建应用程序我在解决如何以智能方式访问DbContext时遇到了一些麻烦。
As I understand in the newer versions of Ninject only constructor injection is encouraged. 据我了解,在较新版本的Ninject中,仅鼓励使用构造函数注入。 As EF 6, itself is repo and unit of work I'm not doing any abstractions on top of EF.
作为EF 6,它本身就是存储库和工作单元,因此我不在EF上进行任何抽象。 If would like to be able to use multiple small units of works so injecting the DbContext (uow) into every class that needs it is not going to work.
如果希望能够使用多个小单元作品,那么将DbContext(uow)注入到需要它的每个类中是行不通的。 In a non IoC way I would do like this:
以一种非IoC的方式,我会这样:
Using(var db = new DbContext){}
How do achieve this using Ninject as I no longer can do kernel.get in my using block... 如何使用Ninject实现此目的,因为我不再可以在我的using块中执行kernel.get ...
I'd consider two approaches: 我会考虑两种方法:
Create general DbContext, which can be hidden behind an interface: 创建常规的DbContext,可以将其隐藏在接口后面:
public interface IPortalContext : IDisposable { DbSet<User> Users { get; } DbContext Context { get; } } public class PortalContext : DbContext, IPortalContext { public PortalContext() : base("PortalConnectionString") { } public virtual DbSet<User> Users { get; set; } }
then you can inject your context to the constructor without problem. 那么您可以毫无问题地将上下文注入构造函数。
Create many small contexts which can be used in different scenarios and classes. 创建许多可以在不同场景和类中使用的小上下文。
I don't think that first approach is bad since it only encapsulates your DbSets
and DbContext
making it easier to inject and test. 我不认为第一种方法是不好的,因为它只封装了
DbSets
和DbContext
使注入和测试更加容易。 You don't make any unnecessary layers above EF and whole interface seems quite transparent. 您无需在EF之上添加任何不必要的层,并且整个界面似乎非常透明。
Anyway this approach is better than making whole IRepository<T>
stuff to access another repository... 无论如何,这种方法比使整个
IRepository<T>
东西访问另一个存储库更好。
I'm not sure what you mean by "multiple small unit of works", but just for exposure, this is what I've done in a recent application: 我不确定您所说的“多个小作品”是什么意思,但是仅仅为了曝光,这就是我在最近的应用程序中所做的:
This is an example of a repository factory 这是存储库工厂的示例
public class CartRepositoryFactory : IRepositoryFactory { public IRepository Generate(CartContext ctx) { return new CartRepository(ctx); 公共类CartRepositoryFactory:IRepositoryFactory {public IRepository Generate(CartContext ctx){返回新的CartRepository(ctx); } }
}}
At the application service layer, I inject a UoW and the repository factory that I need 在应用程序服务层,我注入了我需要的UoW和存储库工厂
You might be asking, but why?!? 您可能会问,但是为什么呢? This is madness!!
这太疯狂了!!
Well, because if the Repository manages the DbContext, then I can only do one operation per class instantiation. 好吧,因为如果存储库管理DbContext,则每个类实例只能执行一项操作。 This allows me to open a DbContext and make several calls to the Repository.
这使我可以打开DbContext并多次调用存储库。 Of course now you have the same problem at application service level, you can only call one method per instantiation, but it's far easier to manage.
当然,现在在应用程序服务级别存在相同的问题,每个实例只能调用一个方法,但是管理起来要容易得多。
Ultimately it all comes down to your taste: would you rather have a thin service or a thin repository? 最终,这一切都取决于您的口味:您是希望拥有瘦服务还是瘦存储库?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.