库和工作单位 - 这是负责什么? [英] Repositories and Units of Work - which is responsible for what?
问题描述
在过去一周左右,我一直在阅读大量的文章和教程有关存储库的模式。很多物品的存储库模式紧密地结合到工作模式的单位。在这些文章中,我通常会发现类似这样的代码:
Over the past week or so I have been reading a lot of articles and tutorials regarding the repository pattern. A lot of the articles closely tie the repository pattern to the unit of work pattern. In these articles, I usually find code similar to this:
interface IUnitOfWork<TEntity>
{
void RegisterNew(TEntity entity);
void RegisterDirty(TEntity entity);
void RegisterDeleted(TEntity entity);
void Commit();
void Rollback();
}
interface IRepository<TKey, TEntity>
{
TEntity FindById(TKey id);
IEnumerable<TEntity> FindAll();
void Add(TEntity entity);
void Update(TEntity entity);
void Delete(TEntity entity);
}
class Repository : IRepository<int, string>
{
public Repository(IUnitOfWork<string> context)
{
this.context = context;
}
private IUnitOfWork<string> context;
public void Add(string entity)
{
context.RegisterNew(entity);
}
public void Update(string entity)
{
context.RegisterDirty(entity);
}
public void Delete(string entity)
{
context.RegisterDeleted(entity);
}
/* Entity retrieval methods */
}
我是否理解正确的工作目标的单位是为了处理另外,更新或任何对象的缺失底层数据存储(在我的情况,目录服务,我通过LDAP与沟通)?如果这是真的,不应该就处理任何对象的检索呢?这是为什么不建议UOW界面的一部分?
Am I understanding correctly that the unit of work object is meant to handle the addition, update, or deletion of any object in the underlying data store (in my case, a directory service which I communicate with via LDAP)? If that's true, shouldn't it handle the retrieval of any objects as well? Why is that not part of the suggested UoW interface?
推荐答案
A 库
负责数据 - 得到它,更新和其他CRUD操作,提供了持久性的无知。
A Repository
is in charge of the data - getting it, updating and the other CRUD operations, providing persistence ignorance.
A 组的工作
(UOW),因为马林福勒说:
A Unit Of Work
(uow), as Marin Fowler says:
保持受业务交易对象的列表,并协调书写出的变化和并发问题的解决。
Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems.
该UOW将协调在对象上的多个操作 - 它可能会或可能不会以保存这些变化使用库
The uow will coordinate multiple operations on objects - it may or may not use repositories in order to persist these changes.
这篇关于库和工作单位 - 这是负责什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!