简体   繁体   English

Ninject和DbContext

[英]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: 我会考虑两种方法:

  1. 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. 那么您可以毫无问题地将上下文注入构造函数。

  2. 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. 我不认为第一种方法是不好的,因为它只封装了DbSetsDbContext使注入和测试更加容易。 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: 我不确定您所说的“多个小作品”是什么意思,但是仅仅为了曝光,这就是我在最近的应用程序中所做的:

  1. Divided the domain in small bounded contexts (this is more of a conceptual step) 在小范围的上下文中划分域(这更多是一个概念上的步骤)
  2. Each bounded context has: a context, a repository, a repository factory 每个有界上下文都有:一个上下文,一个存储库,一个存储库工厂
  3. Each context implements an IContext and a BaseContext that gives basic methods and common properties (IContext will be useful for mocking) 每个上下文都实现一个IContext和一个BaseContext,后者提供基本方法和通用属性(IContext将对模拟很有用)
  4. Each repository takes the relative context as a constructor paramenter 每个存储库都将相对上下文用作构造函数参数
  5. 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); } } }}

  6. At the application service layer, I inject a UoW and the repository factory that I need 在应用程序服务层,我注入了我需要的UoW和存储库工厂

  7. If I want to work with several different context in one service, I simply create another service and combine the services that I need, injecting them 如果我想在一个服务中使用几种不同的上下文,则只需创建另一个服务,然后组合所需的服务,然后将它们注入

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM