使用NHibernate的DDD存储库模式

Mor*_*rph 6 nhibernate domain-driven-design repository

我糊涂了.这是Ayende Rahien Repository的博客文章,是新的单身人士.

我相信存储库应该只执行CRUD操作而不是附加查询,否则你最终会在存储库中使用这些方法.

  1. FindCustomer(ID)
  2. FindCustomerWithAddresses(ID)
  3. FindCustomerWith ..

所以我的问题是,在哪里(在什么层)查询检索实体?

Mar*_*kan 3

可以编写具有默认 CRUD 操作的存储库。例如:

public interface IRepository<TEntity>
{
   TEntity FindByIdentity(object identity);
   TEntity FindBy(Expression<Func<TEntity, bool>> specification);
   IList<TEntity> FindAll();
   IList<TEntity> FindAllBy(Expression<Func<TEntity, bool>> specification);
   TEntity Save(TEntity saveable);
   void Delete(TEntity deletable);
}
Run Code Online (Sandbox Code Playgroud)

Expression> 基本上是规范,查询可以通过这种方式封装。如果我们有这样的存储库,那么我们就不需要编写许多特定的存储库。

替代路径是创建查询对象。我们可以将该查询的接口添加到核心/业务逻辑层,并将实现添加到服务/数据层。这样我们就有了像AllPreferredCustomersQuery这样很好命名的查询。它与规范非常相似,但规范不使用基础设施,因此我们可以将其添加到核心/业务逻辑层。查询对象更加可配置(例如可以添加限制、获取策略、连接等)