Enterprise Integration Zone is brought to you in partnership with:

.NET developer/architect. Been coding since 1996. Jonas is a DZone MVB and is not an employee of DZone and has posted 14 posts at DZone. You can read more from them at their website. View Full User Profile

Repository Pattern, Done Right

02.04.2013
| 3963 views |
  • submit to reddit
The repository pattern has been discussed a lot lately. Especially about it’s usefulness since the introduction of OR/M libraries. This post (which is the third in a series about the data layer) aims to explain why it’s still a great choice.

Let’s start with the definition:

A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes


The repository pattern is used to create an abstraction between your domain and data layer. That is, when you use the repository you should not have to have any knowledge about the underlying data source or the data layer (i.e. Entity Framework, nhibernate or similar).

Why do we need it?

Read the abstractions part of my data layer article. It explains the basics to why we should use repositories or similar abstractions.

But let’s also examine some simple business logic:

var brokenTrucks = _session.Query<Truck>().Where(x => x.State == 1);
foreach (var truck in brokenTrucks)
{
   if (truck.CalculateReponseTime().TotalDays > 30)
       SendEmailToManager(truck);
}

What does that give us? Broken trucks?

Well. No. The statement was copied from another place in the code and the developer had forgot to update the query. Any unit tests would likely just check that some trucks are returned and that they are emailed to the manager.

So we basically have two problems here:

a) Most developers will likely just check the name of the variable and not on the query.
b) Any unit tests are against the business logic and not the query.

Both those problems would have been fixed with repositories. Since if we create repositories we also have unit tests which targets the data layer only.

Implementations

Here are some different implementations with descriptions.

Base classes

These classes can be reused for all different implementations.

UnitOfWork

The unit of work represents a transaction when used in data layers. Typically the unit of work will roll back the transaction if SaveChanges() has not been invoked before being disposed.

public interface IUnitOfWork : IDisposable
{
    void SaveChanges();
}

Paging

We also need to have page results.

public class PagedResult<TEntity>
{
    IEnumerable<TEntity> _items;
    int _totalCount;
     
    public PagedResult(IEnumerable<TEntity> items, int totalCount)
    {
        _items = items;
        _totalCount = totalCount;
    }
     
    public IEnumerable<TEntity> Items { get { return _items; } }
    public int TotalCount { get { return _totalCount; } }
}

We can with the help of that create methods like:
public class UserRepository
{
    public PagedResult<User> Find(int pageNumber, int pageSize)
    {
    }
}

Sorting

Finally we prefer to do sorting and page items, right?

var constraints = new QueryConstraints<User>()
    .SortBy("FirstName")
    .Page(1, 20);
     
var page = repository.Find("Jon", constraints);

Do note that I used the property name, but I could also have written constraints.SortBy(x => x.FirstName). However, that is a bit hard to write in web applications where we get the sort property as a string.

The class is a bit big, but you can find it at github.

In our repository we can apply the constraints as (if it supports LINQ):

public class UserRepository
{
    public PagedResult<User> Find(string text, QueryConstraints<User> constraints)
    {
        var query = _dbContext.Users.Where(x => x.FirstName.StartsWith(text) || x.LastName.StartsWith(text));
        var count = query.Count();
         
        //easy
        var items = constraints.ApplyTo(query).ToList();
         
        return new PagedResult(items, count);
    }
}

The extension methods are also available at github.

Basic contract

I usually start use a small definition for the repository, since it makes my other contracts less verbose. Do note that some of my repository contracts do not implement this interface (for instance if any of the methods do not apply).

public interface IRepository<TEntity, in TKey> where TEntity : class
{
    TEntity GetById(TKey id);
    void Create(TEntity entity);
    void Update(TEntity entity);
    void Delete(TEntity entity);
}

I then specialize it per domain model:

public interface ITruckRepository : IRepository<Truck, string>
{
    IEnumerable<Truck> FindBrokenTrucks();
    IEnumerable<Truck> Find(string text);
}

That specialization is important. It keeps the contract simple. Only create methods that you know that you need.

Entity framework

Do note that the repository pattern is only useful if you have POCOs which are mapped using code first. Otherwise you’ll just break the abstraction using the entities. The repository pattern isn’t very useful then.

What I mean is that if you use the model designer you’ll always get a perfect representation of the database (but as classes). The problem is that those classes might not be a perfect representation of your domain model. Hence you got to cut corners in the domain model to be able to use your generated db classes.

If you on the other hand uses Code First you can modify the models to be a perfect representation of your domain model (if the DB is reasonable similar to it). You don’t have to worry about your changes being overwritten as they would have been by the model designer.

You can follow this article if you want to get a foundation generated for you.

Base class

public class EntityFrameworkRepository<TEntity, TKey> where TEntity : class
{
    private readonly DbContext _dbContext;
 
    public EntityFrameworkRepository(DbContext dbContext)
    {
        if (dbContext == null) throw new ArgumentNullException("dbContext");
        _dbContext = dbContext;
    }
 
    protected DbContext DbContext
    {
        get { return _dbContext; }
    }
 
    public void Create(TEntity entity)
    {
        if (entity == null) throw new ArgumentNullException("entity");
        DbContext.Set<TEntity>().Add(entity);
    }
 
    public TEntity GetById(TKey id)
    {
        return _dbContext.Set<TEntity>().Find(id);
    }
 
    public void Delete(TEntity entity)
    {
        if (entity == null) throw new ArgumentNullException("entity");
        DbContext.Set<TEntity>().Attach(entity);
        DbContext.Set<TEntity>().Remove(entity);
    }
 
    public void Update(TEntity entity)
    {
        if (entity == null) throw new ArgumentNullException("entity");
        DbContext.Set<TEntity>().Attach(entity);
        DbContext.Entry(entity).State = EntityState.Modified;
    }
}

Then I go about and do the implementation:

public class TruckRepository : EntityFrameworkRepository<Truck, string>, ITruckRepository
{
    private readonly TruckerDbContext _dbContext;
 
    public TruckRepository(TruckerDbContext dbContext)
    {
        _dbContext = dbContext;
    }
 
    public IEnumerable<Truck> FindBrokenTrucks()
    {
        //compare having this statement in a business class compared
        //to invoking the repository methods. Which says more?
        return _dbContext.Trucks.Where(x => x.State == 3).ToList();
    }
 
    public IEnumerable<Truck> Find(string text)
    {
        return _dbContext.Trucks.Where(x => x.ModelName.StartsWith(text)).ToList();
    }
}

Unit of work

The unit of work implementation is simple for Entity framework:

public class EntityFrameworkUnitOfWork : IUnitOfWork
{
    private readonly DbContext _context;
 
    public EntityFrameworkUnitOfWork(DbContext context)
    {
        _context = context;
    }
 
    public void Dispose()
    {
         
    }
 
    public void SaveChanges()
    {
        _context.SaveChanges();
    }
}

nhibernate

I usually use fluent nhibernate to map my entities. imho it got a much nicer syntax than the built in code mappings. You can use nhibernate mapping generator to get a foundation created for you. But you do most often have to clean up the generated files a bit.

Base class

public class NHibernateRepository<TEntity, in TKey> where TEntity : class
{
    ISession _session;
     
    public NHibernateRepository(ISession session)
    {
        _session = session;
    }
     
    protected ISession Session { get { return _session; } }
     
    public TEntity GetById(string id)
    {
        return _session.Get<TEntity>(id);
    }
 
    public void Create(TEntity entity)
    {
        _session.SaveOrUpdate(entity);
    }
 
    public void Update(TEntity entity)
    {
        _session.SaveOrUpdate(entity);
    }
 
    public void Delete(TEntity entity)
    {
        _session.Delete(entity);
    }
}

Implementation

public class TruckRepository : NHibernateRepository<Truck, string>, ITruckRepository
{
    public TruckRepository(ISession session)
        : base(session)
    {
    }
 
    public IEnumerable<Truck> FindBrokenTrucks()
    {
        return _session.Query<Truck>().Where(x => x.State == 3).ToList();
    }
 
    public IEnumerable<Truck> Find(string text)
    {
        return _session.Query<Truck>().Where(x => x.ModelName.StartsWith(text)).ToList();
    }
}

Unit of work

public class NHibernateUnitOfWork : IUnitOfWork
{
    private readonly ISession _session;
    private ITransaction _transaction;
 
    public NHibernateUnitOfWork(ISession session)
    {
        _session = session;
        _transaction = _session.BeginTransaction();
    }
 
    public void Dispose()
    {
        if (_transaction != null)
            _transaction.Rollback();
    }
 
    public void SaveChanges()
    {
        if (_transaction == null)
            throw new InvalidOperationException("UnitOfWork have already been saved.");
 
        _transaction.Commit();
        _transaction = null;
    }
}

Typical mistakes

Here are some mistakes which can be stumbled upon when using OR/Ms.

Do not expose LINQ methods

Let’s get it straight. There are no complete LINQ to SQL implementations. They all are either missing features or implement things like eager/lazy loading in their own way. That means that they all are leaky abstractions. So if you expose LINQ outside your repository you get a leaky abstraction. You could really stop using the repository pattern then and use the OR/M directly.

public interface IRepository<TEntity>
{
    IQueryable<TEntity> Query();
     
    // [...]
}

Those repositories really do not serve any purpose. They are just lipstick on a pig (yay, my favorite)

The pig is back!

Those who use them probably don’t want to face the truth:

Bush FTW

or are just not reading very good:

Book is upside down..

Learn about lazy loading

Lazy loading can be great. But it’s a curse for all which are not aware of it. If you don’t know what it is, Google.

If you are not careful you could get 101 executed queries instead of 1 if you traverse a list of 100 items.

Invoke ToList() before returning

The query is not executed in the database until you invoke ToList(), FirstOrDefault() etc. So if you want to be able to keep all data related exceptions in the repositories you have to invoke those methods.

Get is not the same as search

There are to types of reads which are made in the database.

The first one is to search after items. i.e. the user want to identify the items that he/she like to work with.

The second one is when the user has identified the item and want to work with it.

Those queries are different. In the first one, the user only want’s to get the most relevant information. In the second one, the user likely want’s to get all information. Hence in the former one you should probably return UserListItem or similar while the other case returns User. That also helps you to avoid the lazy loading problems.

I usually let search methods start with FindXxxx() while those getting the entire item starts with GetXxxx(). Also don’t be afraid of creating specialized POCOs for the searches. Two searches doesn’t necessarily have to return the same kind of entity information.

Summary

Don’t be lazy and try to make too generic repositories. It gives you no upsides compared to using the OR/M directly. If you want to use the repository pattern, make sure that you do it properly.




Published at DZone with permission of Jonas Gauffin, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)