Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD Repositories pattern with NHibernate [closed]

Im confused. This is a blog entry of Ayende Rahien Repository is the new singleton.

I believe that a repository should only do CRUD operations and not addtional queries otherwise youll end up with methods like these on your repository.

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

So my question is, where(in what layer) would one do the queries to retrieve entities?

like image 728
Morph Avatar asked Nov 03 '09 10:11

Morph


1 Answers

It's possible to write repository which have default CRUD operations. For example:

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);
}

Expression> is basically Specification and queries can be encapsulate that way. If we have that kind of Repository then we don't need to write many specific repositories.

Alternative path is create Query object. We could add interface of that query to Core/Busines Logic layer and implementation to Services/Data layer. That way we have nicely named queries like AllPreferredCustomersQuery. It's quite similar to specifications, but specifications don't use infrastructure and therefore we may add it to Core/Business Logic layer. Query objects are more configurable (e.g. possible to add limits, fetching strategies, joins etc.)

like image 192
Marek Tihkan Avatar answered Oct 06 '22 00:10

Marek Tihkan