简体   繁体   English

在此UnitOfWork实现中注册存储库的目的

[英]Purpose of registering repositories in this UnitOfWork implementation

I am reading an old code using a UnitOfWork implementation which is coded like this: 我正在阅读使用UnitOfWork实现的旧代码,其编码如下:

public class UnitOfWork : IUnitOfWork
{
    private MainContext _context = new MainContext();
    private Dictionary<string, IRepository> _repositories;

    public UnitOfWork(MainContext context)
    {
        _context = context;
        _repositories = new Dictionary<string, IRepository>();
    }

    public void Register(IRepository repository)
    {
        _repositories.Add(repository.GetType().Name, repository);
    }

    public void Commit()
    {
        _context.SaveChanges();
    }
}

On the repository side, the unit of work and context is both constructor-injected. 在存储库方面,工作单元和上下文都是注入构造函数的。 Here are 2 of those repositories: 这是其中两个存储库:

public class RegistrationRepository : IRegistrationRepository
{
    private MainContext _context = new MainContext();
    private UnitOfWork _uow = new UnitOfWork();

    public RegistrationRepository(IUnitOfWork uow, MainContext context)
    {
        _context = context;
        _uow = uow;
    }

    // code omitted
}

public class EmployeeRepository : IEmployeeRepository
{
    private MainContext _context = new MainContext();
    private UnitOfWork _uow = new UnitOfWork();

    public EmployeeRepository(IUnitOfWork uow, MainContext context)
    {
        _context = context;
        _uow = uow;
    }

    // code omitted
}

If I understand correctly, when I inject both these repositories in a Business Logic class, they will both use the same context and so will the UnitOfWork, thus I can have transactionality when calling uow.Commit(). 如果我理解正确,那么当我将这两个存储库注入到Business Logic类中时,它们将都使用相同的上下文,而UnitOfWork也将使用相同的上下文,因此在调用uow.Commit()时我可以具有事务性。

What I want to ask is what the uow.Register()'s purpose is? 我想问的是uow.Register()的目的是什么? I've seen some UoW implementations that have a Commit() method with this code: 我已经看到一些具有以下代码的Commit()方法的UoW实现:

public void Commit()
{
    _repositories.ToList().ForEach(x => x.Value.Submit());
}

This I understand the Register since the Commit is looping through all Repositories. 由于提交遍历所有存储库,因此我理解寄存器。 But on the first example, we just SaveChanges to context so I'm not sure Register is needed. 但是在第一个示例中,我们只是将ChangeSaves保存到上下文,所以我不确定是否需要Register。

To sum up, I have 3 questions: 总结起来,我有3个问题:

  1. What's the purpose of Register method in the UoW implementation in the top-most if I can have transaction using context.SaveChanges() without registering the repositories? 如果我可以在不注册存储库的情况下使用context.SaveChanges()进行事务,那么最顶端的UoW实现中Register方法的目的是什么?

  2. In the last example code where there is a loop through all the repository, will that really be transactional (all succeeds or all fails)? 在最后一个示例代码中,所有存储库都有一个循环,这真的是事务性的(全部成功还是全部失败)?

  3. Which is more preferred? 哪个更优选? Also can you please post what the possible implementation for Submit() would be? 还可以请您发布Submit()的可能实现方式吗? I saw this example in this link 我在这个链接中看到了这个例子

Dependency injection in unit of work pattern using repositories 使用存储库以工作单元为单位进行依赖注入

but the accepted answer only has Submit() defined in IRepository but in his concrete Repository, its missing. 但是接受的答案只有在IRepository中定义了Submit(),但在其具体的Repository中定义了它(缺少)。

  1. The purpose I suppose is to create only one repository per entity, so that you can reuse same repository later by retrieving it from UoW by key. 我想的目的是每个实体仅创建一个存储库,以便以后可以通过按密钥从UoW检索同一存储库来重用该存储库。
  2. If it uses different contexts - no. 如果它使用不同的上下文-不。
  3. Usually you want to have one transaction per business action so only one commit. 通常,您希望每个业务操作进行一次事务,因此仅一次提交。

Here is a good long post about dbContext and approaches. 是一篇有关dbContext和方法的好文章。

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

相关问题 工作单元,存储库和连接的处置 - unitofwork, repositories and disposing of connections 使用IoC注册通用UnitOfWork <>时出错 - Error registering generic UnitOfWork<> with IoC 在 UnitOfWork 中存储对通用存储库的引用 - Store References to Generic Repositories in UnitOfWork C#对UnitOfWork /通用存储库的反思 - C# Reflection on UnitOfWork / Generic Repositories 在实体框架中正确实现UnitOfWork和存储库模式 - Correct implementation of UnitOfWork and Repository pattern in Entity Framework 设计模式:使用IoC设置控制器,服务,存储库和UnitOfWork - Design Pattern: Setting up Controllers, Service, Repositories and UnitOfWork with IoC 如何使我的存储库和工作单元脱钩以使其可测试 - How to decouple my repositories and unitofwork in order to make them testable 城堡-注册未知的实现 - Castle - Registering unknown implementation 在Unity IoC上注册所有存储库和服务 - Registering all repositories and services on Unity IoC 使用 Begintransaction、多个存储库、SQLite、UnitOfWork 和 Dapper 时出现数据库锁定错误 - Database locked error when using Begintransaction, multiple repositories, SQLite, UnitOfWork and Dapper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM