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
1: public interface IRepository : IDisposable
2: { 3: T Get<T>(int id) where T : IEntityWithKey;
4: T Get<T>(Expression<Func<T, bool>> predicate) where T : IEntityWithKey;
5:
6: IQueryable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : IEntityWithKey;
7:
8: T Save<T>(T entity) where T : IEntityWithKey;
9: T Delete<T>(T entity) where T : IEntityWithKey;
10: }
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 itself:
1: public class EntityFrameworkRepository : IRepository
2: { 3: private readonly ObjectContext context;
4:
5: public EntityFrameworkRepository()
6: { 7: context = new WmdDemoEntities();
8: }
9:
10: public EntityFrameworkRepository(ObjectContext entityContext)
11: { 12: context = entityContext;
13: }
14:
15: public T Get<T>(int id) where T : IEntityWithKey
16: { 17: IEnumerable<KeyValuePair<string, object>> entityKeyValues =
18: new[] { new KeyValuePair<string, object>("Id", id) }; 19:
20: var key = new EntityKey(context.GetEntitySet<T>().Name, entityKeyValues);
21:
22: return (T)context.GetObjectByKey(key);
23: }
24:
25: public T Get<T>(Expression<Func<T, bool>> predicate) where T : IEntityWithKey
26: { 27: return context.CreateQuery<T>("[" + context.GetEntitySet<T>().Name + "]") 28: .Where(predicate)
29: .FirstOrDefault();
30: }
31:
32: public IQueryable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : IEntityWithKey
33: { 34: return context
35: .CreateQuery<T>("[" + context.GetEntitySet<T>().Name + "]") 36: .Where(predicate);
37: }
38:
39: public T Save<T>(T entity) where T : IEntityWithKey
40: { 41: object originalItem;
42:
43: if (context.TryGetObjectByKey(entity.EntityKey, out originalItem))
44: context.ApplyPropertyChanges(entity.EntityKey.EntitySetName, entity);
45: else
46: context.AddObject(entity.EntityKey.EntitySetName, entity);
47:
48: context.SaveChanges();
49:
50: return entity;
51: }
52:
53: public T Delete<T>(T entity) where T : IEntityWithKey
54: { 55: context.DeleteObject(entity);
56: context.SaveChanges();
57:
58: return entity;
59: }
60:
61: public void Dispose()
62: { 63: context.Dispose();
64: }
65: }
Here is an example of usage in a console application
1: class Program
2: { 3: static void Main(string[] args)
4: { 5: IRepository repository = new EntityFrameworkRepository();
6: var page = repository.Get<Page>(x => x.PageId == 1);
7:
8: Console.WriteLine("Title: {0}", page.Title); 9: Console.WriteLine("Content: {0}", page.Content); 10:
11: repository.Dispose();
12:
13: Console.ReadLine();
14: }
15: }
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.
Tags: Repository, ORM, Entity Framework