PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Gradebook.Data/Repository.cs

https://bitbucket.org/academium/gradebook
C# | 122 lines | 104 code | 18 blank | 0 comment | 15 complexity | 57ad81b4cb0c1db6b47ec08d8ae8298a MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using Gradebook.Contracts.Repositories;
  7. namespace Gradebook.Data
  8. {
  9. public class Repository<TEntity, TKey> : IRepository<TEntity, TKey>
  10. where TEntity : class
  11. {
  12. protected readonly DbContext DbContext;
  13. protected readonly DbSet<TEntity> DbSet;
  14. protected Repository(DbContext dbContext)
  15. {
  16. if (dbContext == null)
  17. throw new ArgumentNullException("dbContext");
  18. DbContext = dbContext;
  19. DbSet = DbContext.Set<TEntity>();
  20. }
  21. public virtual TEntity Read(TKey id)
  22. {
  23. return DbSet.Find(id);
  24. }
  25. public virtual bool TryRead(TKey id, out TEntity entity)
  26. {
  27. entity = DbSet.Find(id);
  28. return entity != null;
  29. }
  30. public virtual void Delete(TKey id)
  31. {
  32. var entity = Read(id);
  33. if (entity == null) throw new ArgumentException("id");
  34. var entityToDelete = DbSet.Find(id);
  35. Delete(entityToDelete);
  36. }
  37. public virtual void Create(TEntity entity)
  38. {
  39. DbSet.Add(entity);
  40. }
  41. public virtual void Delete(TEntity entity)
  42. {
  43. var dbEntityEntry = DbContext.Entry(entity);
  44. if (dbEntityEntry.State != EntityState.Deleted)
  45. {
  46. dbEntityEntry.State = EntityState.Deleted;
  47. }
  48. else
  49. {
  50. if (DbContext.Entry(entity).State == EntityState.Detached)
  51. {
  52. DbSet.Attach(entity);
  53. }
  54. DbSet.Remove(entity);
  55. }
  56. }
  57. public virtual void Update(TEntity entity)
  58. {
  59. var dbEntityEntry = DbContext.Entry(entity);
  60. if (dbEntityEntry.State == EntityState.Detached)
  61. {
  62. DbSet.Attach(entity);
  63. }
  64. DbContext.Entry(entity).State = EntityState.Modified;
  65. }
  66. public virtual IQueryable<TEntity> Get(
  67. Expression<Func<TEntity, bool>> filter = null,
  68. Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
  69. string includeProperties = "")
  70. {
  71. IQueryable<TEntity> query = DbSet;
  72. if (filter != null)
  73. {
  74. query = query.Where(filter);
  75. }
  76. query = includeProperties
  77. .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
  78. .Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
  79. return orderBy != null ? orderBy(query) : query;
  80. }
  81. public virtual IQueryable<TEntity> GetAll()
  82. {
  83. return DbSet;
  84. }
  85. public long Count()
  86. {
  87. return DbSet.Count();
  88. }
  89. public virtual bool Contains(TKey id)
  90. {
  91. return DbSet.Find(id) != null;
  92. }
  93. }
  94. public class Repository<TEntity> : Repository<TEntity, int>, IRepository<TEntity>
  95. where TEntity : class
  96. {
  97. public Repository(DbContext dbContext) : base(dbContext) { }
  98. }
  99. public class RepositoryBase<TEntity> : Repository<TEntity, object>, IRepositoryBase<TEntity>
  100. where TEntity : class
  101. {
  102. public RepositoryBase(DbContext dbContext) : base(dbContext) { }
  103. }
  104. }