代码之家  ›  专栏  ›  技术社区  ›  Justin Rusbatch

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

  •  5
  • Justin Rusbatch  · 技术社区  · 14 年前

    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接口的一部分?

    1 回复  |  直到 14 年前
        1
  •  4
  •   Oded    14 年前

    Repository

    Unit Of Work (uow),正如Marin Fowler所说:

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