|
| 1 | +namespace DotNetToolkit.Repository.EntityFrameworkCore |
| 2 | +{ |
| 3 | + using FetchStrategies; |
| 4 | + using Microsoft.EntityFrameworkCore; |
| 5 | + using Queries; |
| 6 | + using Specifications; |
| 7 | + using System; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.Linq; |
| 10 | + using System.Linq.Expressions; |
| 11 | + using System.Threading; |
| 12 | + using System.Threading.Tasks; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Represents a repository for entity framework core. |
| 16 | + /// </summary> |
| 17 | + public abstract class EfCoreRepositoryBase<TEntity, TKey> : RepositoryAsyncBase<TEntity, TKey> where TEntity : class |
| 18 | + { |
| 19 | + #region Fields |
| 20 | + |
| 21 | + private bool _disposed; |
| 22 | + |
| 23 | + #endregion |
| 24 | + |
| 25 | + #region Properties |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Gets the entity set. |
| 29 | + /// </summary> |
| 30 | + protected DbSet<TEntity> DbSet { get; private set; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Gets the database context. |
| 34 | + /// </summary> |
| 35 | + protected DbContext Context { get; private set; } |
| 36 | + |
| 37 | + #endregion |
| 38 | + |
| 39 | + #region Constructors |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Initializes a new instance of the <see cref="EfCoreRepositoryBase{TEntity, TKey}" /> class. |
| 43 | + /// </summary> |
| 44 | + /// <param name="context">The database context.</param> |
| 45 | + protected EfCoreRepositoryBase(DbContext context) |
| 46 | + { |
| 47 | + if (context == null) |
| 48 | + throw new ArgumentNullException(nameof(context)); |
| 49 | + |
| 50 | + Context = context; |
| 51 | + DbSet = Context.Set<TEntity>(); |
| 52 | + } |
| 53 | + |
| 54 | + #endregion |
| 55 | + |
| 56 | + #region Protected Methods |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Releases unmanaged and - optionally - managed resources. |
| 60 | + /// </summary> |
| 61 | + /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> |
| 62 | + protected virtual void Dispose(bool disposing) |
| 63 | + { |
| 64 | + if (_disposed) return; |
| 65 | + |
| 66 | + if (disposing) |
| 67 | + { |
| 68 | + if (Context != null) |
| 69 | + { |
| 70 | + Context.Dispose(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + _disposed = true; |
| 75 | + } |
| 76 | + |
| 77 | + #endregion |
| 78 | + |
| 79 | + #region Overrides of RepositoryBase<TEntity,TKey> |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// A protected overridable method for adding the specified <paramref name="entity" /> into the repository. |
| 83 | + /// </summary> |
| 84 | + protected override void AddItem(TEntity entity) |
| 85 | + { |
| 86 | + DbSet.Add(entity); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// A protected overridable method for deleting the specified <paramref name="entity" /> from the repository. |
| 91 | + /// </summary> |
| 92 | + protected override void DeleteItem(TEntity entity) |
| 93 | + { |
| 94 | + DbSet.Remove(entity); |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// A protected overridable method for updating the specified <paramref name="entity" /> in the repository. |
| 99 | + /// </summary> |
| 100 | + protected override void UpdateItem(TEntity entity) |
| 101 | + { |
| 102 | + Context.Entry(entity).State = EntityState.Modified; |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// A protected overridable method for saving changes made in the current unit of work in the repository. |
| 107 | + /// </summary> |
| 108 | + protected override void SaveChanges() |
| 109 | + { |
| 110 | + Context.SaveChanges(); |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// A protected overridable method for getting an entity query that supplies the specified fetching strategy from the repository. |
| 115 | + /// </summary> |
| 116 | + protected override IQueryable<TEntity> GetQuery(IFetchStrategy<TEntity> fetchStrategy = null) |
| 117 | + { |
| 118 | + var query = DbSet.AsQueryable(); |
| 119 | + return fetchStrategy == null ? query : fetchStrategy.IncludePaths.Aggregate(query, (current, path) => current.Include(path)); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Gets an entity query with the given primary key value from the repository. |
| 124 | + /// </summary> |
| 125 | + protected override TEntity GetEntity(TKey key, IFetchStrategy<TEntity> fetchStrategy) |
| 126 | + { |
| 127 | + return fetchStrategy == null ? DbSet.Find(key) : base.GetEntity(key, fetchStrategy); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| 132 | + /// </summary> |
| 133 | + public override void Dispose() |
| 134 | + { |
| 135 | + Dispose(true); |
| 136 | + GC.SuppressFinalize(this); |
| 137 | + } |
| 138 | + |
| 139 | + #endregion |
| 140 | + |
| 141 | + #region Overrides of RepositoryAsyncBase<TEntity,TKey> |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// A protected asynchronous overridable method for saving changes made in the current unit of work in the repository. |
| 145 | + /// </summary> |
| 146 | + protected override Task SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken()) |
| 147 | + { |
| 148 | + return Context.SaveChangesAsync(cancellationToken); |
| 149 | + } |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Gets an entity query with the given primary key value from the repository. |
| 153 | + /// </summary> |
| 154 | + protected override Task<TEntity> GetEntityAsync(TKey key, IFetchStrategy<TEntity> fetchStrategy, CancellationToken cancellationToken = new CancellationToken()) |
| 155 | + { |
| 156 | + return fetchStrategy == null ? DbSet.FindAsync(new object[] { key }, cancellationToken) : base.GetEntityAsync(key, fetchStrategy, cancellationToken); |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// A protected asynchronous overridable method for getting an entity that satisfies the criteria specified by the <paramref name="criteria" /> from the repository. |
| 161 | + /// </summary> |
| 162 | + protected override Task<TEntity> GetEntityAsync(ISpecification<TEntity> criteria, IQueryOptions<TEntity> options, CancellationToken cancellationToken = new CancellationToken()) |
| 163 | + { |
| 164 | + if (criteria == null) |
| 165 | + throw new ArgumentNullException(nameof(criteria)); |
| 166 | + |
| 167 | + return GetQuery(criteria, options).FirstOrDefaultAsync(cancellationToken); |
| 168 | + } |
| 169 | + |
| 170 | + /// <summary> |
| 171 | + /// A protected asynchronous overridable method for getting an entity that satisfies the criteria specified by the <paramref name="criteria" /> from the repository. |
| 172 | + /// </summary> |
| 173 | + protected override Task<TResult> GetEntityAsync<TResult>(ISpecification<TEntity> criteria, IQueryOptions<TEntity> options, Expression<Func<TEntity, TResult>> selector, CancellationToken cancellationToken = new CancellationToken()) |
| 174 | + { |
| 175 | + if (criteria == null) |
| 176 | + throw new ArgumentNullException(nameof(criteria)); |
| 177 | + |
| 178 | + return GetQuery(criteria, options).Select(selector).FirstOrDefaultAsync(cancellationToken); |
| 179 | + } |
| 180 | + |
| 181 | + /// <summary> |
| 182 | + /// A protected asynchronous overridable method for getting a collection of entities that satisfies the criteria specified by the <paramref name="criteria" /> from the repository. |
| 183 | + /// </summary> |
| 184 | + protected override Task<List<TEntity>> GetEntitiesAsync(ISpecification<TEntity> criteria, IQueryOptions<TEntity> options, CancellationToken cancellationToken = new CancellationToken()) |
| 185 | + { |
| 186 | + if (criteria == null) |
| 187 | + throw new ArgumentNullException(nameof(criteria)); |
| 188 | + |
| 189 | + return GetQuery(criteria, options).ToListAsync(cancellationToken); |
| 190 | + } |
| 191 | + |
| 192 | + /// <summary> |
| 193 | + /// A protected asynchronous overridable method for getting a collection of entities that satisfies the criteria specified by the <paramref name="criteria" /> from the repository. |
| 194 | + /// </summary> |
| 195 | + protected override Task<List<TResult>> GetEntitiesAsync<TResult>(ISpecification<TEntity> criteria, IQueryOptions<TEntity> options, Expression<Func<TEntity, TResult>> selector, CancellationToken cancellationToken = new CancellationToken()) |
| 196 | + { |
| 197 | + if (criteria == null) |
| 198 | + throw new ArgumentNullException(nameof(criteria)); |
| 199 | + |
| 200 | + return GetQuery(criteria, options).Select(selector).ToListAsync(cancellationToken); |
| 201 | + } |
| 202 | + |
| 203 | + /// <summary> |
| 204 | + /// A protected asynchronous overridable method for getting a the number of entities that satisfies the criteria specified by the <paramref name="criteria" /> from the repository. |
| 205 | + /// </summary> |
| 206 | + protected override Task<int> GetCountAsync(ISpecification<TEntity> criteria, CancellationToken cancellationToken = new CancellationToken()) |
| 207 | + { |
| 208 | + var predicate = criteria?.Predicate; |
| 209 | + |
| 210 | + return predicate == null ? GetQuery().CountAsync(cancellationToken) : GetQuery().CountAsync(predicate, cancellationToken); |
| 211 | + } |
| 212 | + |
| 213 | + /// <summary> |
| 214 | + /// A protected asynchronous overridable method for determining whether the repository contains an entity that satisfies the criteria specified by the <paramref name="criteria" /> from the repository. |
| 215 | + /// </summary> |
| 216 | + protected override Task<bool> GetExistAsync(ISpecification<TEntity> criteria, CancellationToken cancellationToken = new CancellationToken()) |
| 217 | + { |
| 218 | + if (criteria == null) |
| 219 | + throw new ArgumentNullException(nameof(criteria)); |
| 220 | + |
| 221 | + var predicate = criteria?.Predicate; |
| 222 | + |
| 223 | + return predicate == null ? GetQuery().AnyAsync(cancellationToken) : GetQuery().AnyAsync(predicate, cancellationToken); |
| 224 | + } |
| 225 | + |
| 226 | + #endregion |
| 227 | + } |
| 228 | +} |
0 commit comments