[英]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个问题:
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方法的目的是什么?
In the last example code where there is a loop through all the repository, will that really be transactional (all succeeds or all fails)? 在最后一个示例代码中,所有存储库都有一个循环,这真的是事务性的(全部成功还是全部失败)?
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中定义了它(缺少)。
Here is a good long post about dbContext and approaches. 这是一篇有关dbContext和方法的好文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.