简体   繁体   English

如何使用工作单元实例化依赖项注入?

[英]How dependency injection is instantiated using Unit of Work?

I was looking to implement the Repository pattern with unit of work in my asp.net core 2.0 app. 我一直希望在asp.net core 2.0应用程序中以工作单元实现存储库模式。 There are not many examples of doing this using the dependency injection (DI) principle being used in .net core. 使用.net核心中使用的依赖项注入(DI)原理执行此操作的示例并不多。 I tried to rewrite this example found in the docs. 我试图重写该示例在文档中找到。 I also tried to add async operations where I could. 我还尝试在可能的地方添加异步操作。

Now the idea is that the unit of work passes it's own dbcontext to the GenericRepository for each of the entities that is in use. 现在的想法是,工作单元将其自己的dbcontext传递给每个正在使用的实体的GenericRepository。 This then makes sure that you ony use one dbcontext even if you work on two entities. 这样就可以确保即使在两个实体上工作,也只能使用一个dbcontext。

In my controller I fetch some data like this: 在我的控制器中,我获取了一些这样的数据:

var model = new IndexViewModel
{
      Companies = await _unitOfWork.CompanyRepository.GetAsync()          
};

In my unit of work the dbcontext is being created using DI. 在我的工作单元中,使用DI创建dbcontext。 Then it creates new instances of the GenericRepository for each entity while it passes it's dbcontext to the genericrepository's contructor: 然后,它在将其dbcontext传递给通用存储库的构造函数的同时,为每个实体创建GenericRepository的新实例:

private ApplicationDbContext _context;
private GenericRepository<Company> companyRepository;
private GenericRepository<Person> personRepository;        

public UnitOfWork(ApplicationDbContext context)
{
    _context = context;
}

public GenericRepository<Company> CompanyRepository
{
    get
    {
        if (this.companyRepository == null)
        {
            this.companyRepository = new GenericRepository<Company>(_context);
        }
        return companyRepository;
    }
}

//repeat for Person

But I fear the dependency injection will automatically create a new dbcontext for each time I use the GenericRepository. 但是我担心依赖注入会在每次使用GenericRepository时自动创建一个新的dbcontext。

public class GenericRepository<TEntity> where TEntity : class
{
    internal ApplicationDbContext _context;
    internal DbSet<TEntity> dbSet;

    public GenericRepository(ApplicationDbContext context)
    {
        _context = context;
        dbSet = context.Set<TEntity>();
    }
     //...other methods
}

I fear this will actually create two contexts. 我担心这实际上会创建两个上下文。 one for each(if two requests are made)? 每个一个(如果提出两个请求)? So in reality the dbcontext would be instantiated three times, one in unitofwork then one for each repository? 因此,实际上dbcontext将被实例化三次,以工作单元形式一次,然后为每个存储库一次? Here is a link to the app on github. 这是指向 github上应用程序的链接 It is working, but I want to understand how this works. 它正在工作,但是我想了解它是如何工作的。 Thanks for any answers! 感谢您的任何答案!

It all depends on how you register your DbContext and which life-time you use. 这完全取决于您如何注册DbContext以及使用的时间。

The default overload of .AddDbContext will always register the DbContext with scoped life time. .AddDbContext的默认重载将始终在具有范围的生存DbContext注册DbContext That means, it will create one instance per request. 这意味着它将为每个请求创建一个实例。

If you make it transient , it will create one instance per resolve and singleton one instance per application lifetime. 如果你让短暂的 ,它会创建每决心和每个应用程序生命周期一个实例一个实例。

That should be true for most cases. 在大多数情况下应该是这样。

However, if you have a service which have a higher lifetime then it's dependencies (ie a singleton and inject a scoped service), then the above is not true and you have to be careful when you design and do your registration and take this into consideration. 但是,如果您有一个具有较高生存期的服务,则它是依赖项(即单例并注入一个有范围的服务),那么上述情况就不成立,在设计和注册时必须小心,并考虑到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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