Entity Framework 4 - Part Three : Repository Pattern
Last post showed how to use a ObjectContext derivation to automatically generate audit information. Now instead of using a ObjectContext directly, a rather different approach, a pattern called Repository Pattern is used to encapsulate access to the entities.
I’m using the bits included in Visual Studio 2010 Beta 2 with additional features included in Entity Framework 4 CTP 2 (not included in VS 2010) which you can download here.
What is Specification Pattern and what does it have to do with Repository Pattern? In simple terms, a specification is a piece of business logic that specifies if an entity is matching a certain criteria. Let’s suppose we need to fetch list of high profile Customers. We can hard code this criteria in UI code behind which is a bad practice you probably agree. We can also place this on our entity class, the Customer. This may not seem so bad. Maybe it is even a good practice to build the business logics like these into the entity, right? But this will only lead to a polluted interface on our entity class as more business logic is introduced. Using Specification Pattern and Separation of Concerns, these pieces of business logic can be placed in a separate class where they belong. Later, we’ll pass this criteria as a parameter into the repository to specify which entities we want to be returned.
The Specification interface we’re going to use would look like this:
public interface ISpecification<TEntity> where TEntity : EntityBase
…and implementation for our example above would be this:
public class HighProfileCustomersSpecification : ISpecification<Customer>
First let’s specify an interface for our repository. We need generic method like FindByKey, Save, Update and methods that accept an ISpecification parameter like FindOne and FindMany.
public interface IRepository<TEntity> where TEntity : EntityBase
Let’s start with the easiest one, FindByKey method. To find an entity an ObjectContext that exposes the entity using
IObjectSet<TEntity> is needed so in our generic repository, we’ll create those.
public abstract class Repository<TEntity> : IRepository<TEntity> where TEntity : EntityBase
With this a lot of our implementation would just call the related method on Entity property.
public TEntity FindByKey(long id)
For Save and Update methods, the ObjectContext does provide a method that has a parameter accepting a fully qualified entity name. What’s FQEN, you may ask? Each object that is in a EntityFramework container has a fully qualified name which is in
ContainerName.EntityName format. These are the MetaData information that is exposed through EntitySet property and usually are stored in the .edmx file which we don’t have because we’re using the All Code feature, remember? Fortunately, there is one place that we can get the EntitySet metadata for our POCO entity and that is through the ObjectSet instance.
private string GetEntityName()
Now it is possible to write the generic Save and Update methods as follows:
public TEntity Update(TEntity entity)
I’ve wraped the call to SaveChanges in Save and Update methods. You can provide additional SaveChanges method in the repository that will be called explicitly by the code that uses the repository instance.
For remaining query methods that return one or multiple instances of the Entity, we’ll use the generic CreateQuery method on the ObjectContext and feed the ISpecification’s criteria to the Where method.
public TEntity FindOne(ISpecification<TEntity> spec)
That sums it up. Our concrete repositories are now just a breeze to create. We can build any missing fine grained methods into our concrete repository using the Entity property. With this approach, you only need one Repository per each Aggregate Root and EF4 will take care of any related transient child entities that should be persisted when aggregate root is saved.
public interface ICustomerRepository : IRepository<Customer>
You can get the whole project containing complete API from here.