IRepository using lambda expressions


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // Interface defining all repository functions
  2.  
  3. interface IRepository<EntityType, IdType>
  4. {
  5. IQueryable<EntityType> GetAll();
  6. EntityType GetById(IdType id);
  7. void Add(EntityType newEntity);
  8. void Delete(EntityType entity);
  9. void Save();
  10. }
  11.  
  12. // Repository base class (needs override)
  13.  
  14. public class Repository<EntityType, IdType> : IRepository<EntityType, IdType>
  15. {
  16. protected Func<IQueryable<EntityType>> QueryFunction { set; get; }
  17. protected Func<IdType, EntityType> GetByIdFunction { set; get; }
  18. protected Action SaveAction { set; get; }
  19. protected Action<EntityType> AddAction { set; get; }
  20. protected Action<EntityType> DeleteAction { set; get; }
  21.  
  22. public IQueryable<EntityType> GetAll()
  23. {
  24. if (QueryFunction == null)
  25. throw new RepositoryException("QueryFunction property not set");
  26.  
  27. try
  28. {
  29. return QueryFunction();
  30. }
  31. catch (Exception e)
  32. {
  33. throw new RepositoryException("Error in GetAll", e);
  34. }
  35. }
  36.  
  37. public EntityType GetById(IdType id)
  38. {
  39. if (GetByIdFunction == null)
  40. throw new RepositoryException("GetByIdFunction property not set");
  41.  
  42. try
  43. {
  44. EntityType entity = GetByIdFunction(id);
  45. return entity;
  46. }
  47. catch(Exception e)
  48. {
  49. throw new RepositoryException("Error in GetByID", e);
  50. }
  51. }
  52.  
  53. public void Add(EntityType newEntity)
  54. {
  55. if (AddAction == null)
  56. throw new RepositoryException("AddAction property not set");
  57.  
  58. try
  59. {
  60. AddAction(newEntity);
  61. }
  62. catch(Exception e)
  63. {
  64. throw new RepositoryException("Error in Add", e);
  65. }
  66. }
  67.  
  68. public void Delete(EntityType entity)
  69. {
  70. if (DeleteAction == null)
  71. throw new RepositoryException("DeleteAction property not set");
  72.  
  73. try
  74. {
  75. DeleteAction(entity);
  76. }
  77. catch (Exception e)
  78. {
  79. throw new RepositoryException("Error in Delete", e);
  80. }
  81. }
  82.  
  83. public void Save()
  84. {
  85. if (SaveAction == null)
  86. throw new RepositoryException("SaveAction property not set");
  87.  
  88. try
  89. {
  90. SaveAction();
  91. }
  92. catch (Exception e)
  93. {
  94. throw new RepositoryException("Error in Save", e);
  95. }
  96. }
  97. }
  98.  
  99. public class RepositoryException : Exception
  100. {
  101. public RepositoryException(string message)
  102. : base(message)
  103. {
  104. }
  105.  
  106. public RepositoryException(string message, Exception inner)
  107. : base(message, inner)
  108. {
  109. }
  110. }
  111.  
  112. // Example repository implementation, using Entity Framework as DAL
  113.  
  114. public class PersonRepository : Repository<Person, int>
  115. {
  116. DatabaseEntities _context;
  117. public PersonRepository()
  118. {
  119. _context = new DatabaseEntities();
  120.  
  121. QueryFunction = () => _context.Person;
  122. GetByIdFunction = id => _context.Person.FirstOrDefault(p => p.ID == id);
  123.  
  124. AddAction = p => _context.AddToPerson(p);
  125. DeleteAction = p => _context.DeleteObject(p);
  126. SaveAction = () => _context.SaveChanges();
  127. }
  128. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.