Skip to content
besnik edited this page Apr 27, 2012 · 26 revisions

Generic Repository .NET C#

GenericRepository project is generic implementation of Repository pattern. For detailed discussion please see [Introduction].

Lightweight

It is lightweight thin layer between domain model and data mappers (e.g. ORMs like NHibernate, Linq2Sql or Entity Framework). The goal is to avoid recreating same repositories over and over again in all projects where repository pattern is used. Designed with respect to DDD (domain driven design). Implements Filter pattern and best used with factory and/or Service locator patter (DI/IoC). I used the name specification, but it turned out to be confusing of other design pattern, so from now on I call it filters.

Example of usage:

var customer = new Customer { Name = "Peter Bondra", Age = 37 };
var specificationLocator = this.IoC.Resolve<ISpecificationLocator>();

using ( var unitOfWork = this.IoC.Resolve<IUnitOfWorkFactory>().BeginUnitOfWork() )
{
  ICustomerRepository cr = this.IoC.Resolve<ICustomerRepository>(unitOfWork, specificationLocator);
  
  using ( var transaction = unitOfWork.BeginTransaction() )
  {
    cr.Insert(customer);
    transaction.Commit();
  }
}
Clone this wiki locally