I have started trying to learn how to leverage the Entity Framework for a the re-write of the GiveCamp Starter Site and wanted to implement a generic repository for data access like I use on most of my projects. I started with the example given by Mikael Henriksson’s Post Generic Repository for Entity Framework for Pluralized Entity Set. I modified it to fit my standard Repository interface and took the liberty of simplifying it in a few places.
I have run this through several test and haven’t found any issues. I would love those with a critical eye to give this a look over and let me know if I am missing anything.
The IRepository Interface
public interface IRepository : IDisposable
{
T Get<T>(int id) where T : IEntityWithKey;
T Get<T>(Expression<Func<T, bool>> predicate) where T : IEntityWithKey;
IQueryable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : IEntityWithKey;
T Save<T>(T entity) where T : IEntityWithKey;
T Delete<T>(T entity) where T : IEntityWithKey;
}
I did use the ObjectContextExtensions from Mikael’s post because they did such a good job of solving the issue of getting the EntitySetName. I am not going go into a lot of detail explaining what is going on because a feel the class pretty much explains itself.
The Repository Class:
public class EntityFrameworkRepository : IRepository
{
private readonly ObjectContext context;
public EntityFrameworkRepository()
{
context = new WmdDemoEntities();
}
public EntityFrameworkRepository(ObjectContext entityContext)
{
context = entityContext;
}
public T Get<T>(int id) where T : IEntityWithKey
{
IEnumerable<KeyValuePair<string, object>> entityKeyValues = new[] { new KeyValuePair<string, object>("Id", id) };
var key = new EntityKey(context.GetEntitySet<T>().Name, entityKeyValues);
return (T)context.GetObjectByKey(key);
}
public T Get<T>(Expression<Func<T, bool>> predicate) where T : IEntityWithKey
{
return context.CreateQuery<T>("[" + context.GetEntitySet<T>().Name + "]")
.Where(predicate)
.FirstOrDefault();
}
public IQueryable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : IEntityWithKey
{
return context
.CreateQuery<T>("[" + context.GetEntitySet<T>().Name + "]")
.Where(predicate);
}
public T Save<T>(T entity) where T : IEntityWithKey
{
object originalItem;
if (context.TryGetObjectByKey(entity.EntityKey, out originalItem))
context.ApplyPropertyChanges(entity.EntityKey.EntitySetName, entity);
else
context.AddObject(entity.EntityKey.EntitySetName, entity);
context.SaveChanges();
return entity;
}
public T Delete<T>(T entity) where T : IEntityWithKey
{
context.DeleteObject(entity);
context.SaveChanges();
return entity;
}
public void Dispose()
{
context.Dispose();
}
}
Here is an example of usage in a console application
class Program
{
static void Main(string[] args)
{
IRepository repository = new EntityFrameworkRepository();
var page = repository.Get<Page>(x => x.PageId == 1);
Console.WriteLine("Title: {0}", page.Title);
Console.WriteLine("Content: {0}", page.Content);
repository.Dispose();
Console.ReadLine();
}
}
I love taking this approach to persistence because it is so flexible and allows me to swap out repository implementations with little issue.
Again love to hear some feedback on this approach.