存储库和工作单元 - 负责什么?

发布于 2024-09-07 19:35:47 字数 1160 浏览 9 评论 0原文

在过去的一周左右的时间里,我阅读了很多有关存储库模式的文章和教程。许多文章将存储库模式与工作单元模式紧密联系在一起。在这些文章中,我通常会找到与此类似的代码:

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 界面的一部分?

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 */
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-09-14 19:35:47

存储库负责数据 - 获取数据、更新和其他 CRUD 操作,提供持久性无知。

工作单元 (uow),正如 Marin Fowler 所说:

维护受业务事务影响的对象列表,并协调更改的写出和并发问题的解决。

uow 将协调对象上的多个操作 - 它可能会或可能不会使用存储库来保存这些更改。

A Repository is in charge of the data - getting it, updating and the other CRUD operations, providing persistence ignorance.

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.

The uow will coordinate multiple operations on objects - it may or may not use repositories in order to persist these changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文