|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using DbContextScope.Ef7.Interfaces; |
| 7 | +using Microsoft.Data.Entity; |
| 8 | + |
| 9 | +namespace DbContextScope.UnitOfWork.Ef7.Repository |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Implements the IEntityFrameworkRepository<TEntity> interface to represent an Entity Framework repository. |
| 13 | + /// </summary> |
| 14 | + /// <typeparam name="TEntity"></typeparam> |
| 15 | + /// <typeparam name="TContext"></typeparam> |
| 16 | + public class EntityFrameworkRepository<TEntity, TContext> : IEntityFrameworkRepository<TEntity, TContext> |
| 17 | + where TEntity : class |
| 18 | + where TContext : DbContext |
| 19 | + { |
| 20 | + #region Attributes |
| 21 | + |
| 22 | + readonly IAmbientDbContextLocator dbContextLocator; |
| 23 | + |
| 24 | + #endregion |
| 25 | + |
| 26 | + #region IEntityFrameworkRepository<TEntity, TContext> Implementation |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Constructor that receives a DbContextLocator instance. |
| 30 | + /// </summary> |
| 31 | + /// <param name="dbContextLocator">DbContextLocator instance.</param> |
| 32 | + public EntityFrameworkRepository(IAmbientDbContextLocator dbContextLocator) |
| 33 | + { |
| 34 | + if (dbContextLocator == null) throw new ArgumentNullException(nameof(dbContextLocator)); |
| 35 | + |
| 36 | + this.dbContextLocator = dbContextLocator; |
| 37 | + } |
| 38 | + |
| 39 | + public virtual void Add(TEntity entity) |
| 40 | + { |
| 41 | + GetDbSet().Add(entity); |
| 42 | + } |
| 43 | + |
| 44 | + public IQueryable<TEntity> AsQueryable() |
| 45 | + { |
| 46 | + return GetDbSet().AsQueryable(); |
| 47 | + } |
| 48 | + |
| 49 | + public virtual void Attach(TEntity entity) |
| 50 | + { |
| 51 | + GetDbSet().Attach(entity); |
| 52 | + } |
| 53 | + |
| 54 | + public virtual void Delete(TEntity entity) |
| 55 | + { |
| 56 | + if (GetDbContext().Entry(entity).State == EntityState.Detached) |
| 57 | + { |
| 58 | + GetDbSet().Attach(entity); |
| 59 | + } |
| 60 | + GetDbSet().Remove(entity); |
| 61 | + } |
| 62 | + |
| 63 | + public virtual void Edit(TEntity entity) |
| 64 | + { |
| 65 | + GetDbContext().Entry(entity).State = EntityState.Modified; |
| 66 | + } |
| 67 | + |
| 68 | + public virtual TEntity First(Expression<Func<TEntity, bool>> predicate = null) |
| 69 | + { |
| 70 | + return GetDbSet().First(predicate); |
| 71 | + } |
| 72 | + |
| 73 | + public virtual TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate = null) |
| 74 | + { |
| 75 | + return GetDbSet().FirstOrDefault(predicate); |
| 76 | + } |
| 77 | + |
| 78 | + public virtual async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate = null) |
| 79 | + { |
| 80 | + return await GetDbSet().FirstOrDefaultAsync(predicate); |
| 81 | + } |
| 82 | + |
| 83 | + public virtual IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> predicate = null, |
| 84 | + Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, |
| 85 | + params Expression<Func<TEntity, object>>[] includeProperties) |
| 86 | + { |
| 87 | + return PrepareGetQuery(predicate, orderBy, includeProperties).ToList(); |
| 88 | + } |
| 89 | + |
| 90 | + public virtual async Task<IEnumerable<TEntity>> GetAsync(Expression<Func<TEntity, bool>> predicate = null, |
| 91 | + Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, |
| 92 | + params Expression<Func<TEntity, object>>[] includeProperties) |
| 93 | + { |
| 94 | + return await PrepareGetQuery(predicate, orderBy, includeProperties).ToListAsync(); |
| 95 | + } |
| 96 | + |
| 97 | + public virtual TEntity LastOrDefault(Expression<Func<TEntity, bool>> predicate = null) |
| 98 | + { |
| 99 | + return GetDbSet().LastOrDefault(predicate); |
| 100 | + } |
| 101 | + |
| 102 | + public virtual TEntity Single(Expression<Func<TEntity, bool>> predicate) |
| 103 | + { |
| 104 | + return GetDbSet().Single(predicate); |
| 105 | + } |
| 106 | + |
| 107 | + public virtual async Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> predicate) |
| 108 | + { |
| 109 | + return await GetDbSet().SingleAsync(predicate); |
| 110 | + } |
| 111 | + |
| 112 | + public virtual TEntity SingleOrDefault(Expression<Func<TEntity, bool>> predicate) |
| 113 | + { |
| 114 | + return GetDbSet().SingleOrDefault(predicate); |
| 115 | + } |
| 116 | + |
| 117 | + public virtual async Task<TEntity> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate) |
| 118 | + { |
| 119 | + return await GetDbSet().SingleOrDefaultAsync(predicate); |
| 120 | + } |
| 121 | + |
| 122 | + public virtual void Update(TEntity entityToUpdate) |
| 123 | + { |
| 124 | + GetDbSet().Attach(entityToUpdate); |
| 125 | + GetDbContext().Entry(entityToUpdate).State = EntityState.Modified; |
| 126 | + } |
| 127 | + |
| 128 | + #endregion |
| 129 | + |
| 130 | + #region Private Methods |
| 131 | + |
| 132 | + DbContext GetDbContext() |
| 133 | + { |
| 134 | + return dbContextLocator.Get<TContext>(); |
| 135 | + } |
| 136 | + |
| 137 | + DbSet<TEntity> GetDbSet() |
| 138 | + { |
| 139 | + return dbContextLocator.Get<TContext>().Set<TEntity>(); |
| 140 | + } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// |
| 144 | + /// </summary> |
| 145 | + /// <param name="predicate"></param> |
| 146 | + /// <param name="orderBy"></param> |
| 147 | + /// <param name="includeProperties"></param> |
| 148 | + /// <returns></returns> |
| 149 | + IQueryable<TEntity> PrepareGetQuery(Expression<Func<TEntity, bool>> predicate = null, |
| 150 | + Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, |
| 151 | + params Expression<Func<TEntity, object>>[] includeProperties) |
| 152 | + { |
| 153 | + var query = AsQueryable(); |
| 154 | + |
| 155 | + if (predicate != null) |
| 156 | + { |
| 157 | + query = query.Where(predicate); |
| 158 | + } |
| 159 | + |
| 160 | + foreach (var includeProperty in includeProperties) |
| 161 | + { |
| 162 | + query = query.Include(includeProperty); |
| 163 | + } |
| 164 | + |
| 165 | + if (orderBy != null) |
| 166 | + { |
| 167 | + return orderBy(query); |
| 168 | + } |
| 169 | + |
| 170 | + return query; |
| 171 | + } |
| 172 | + |
| 173 | + #endregion |
| 174 | + } |
| 175 | +} |
0 commit comments