11namespace DotNetToolkit . Repository . EntityFramework
22{
33 using FetchStrategies ;
4+ using Queries ;
5+ using Specifications ;
46 using System ;
7+ using System . Collections . Generic ;
58 using System . Data . Entity ;
69 using System . Linq ;
10+ using System . Linq . Expressions ;
11+ using System . Threading ;
12+ using System . Threading . Tasks ;
713
814 /// <summary>
915 /// Represents a repository for entity framework.
1016 /// </summary>
11- public abstract class EfRepositoryBase < TEntity , TKey > : RepositoryBase < TEntity , TKey > where TEntity : class
17+ public abstract class EfRepositoryBase < TEntity , TKey > : RepositoryAsyncBase < TEntity , TKey > where TEntity : class
1218 {
1319 #region Fields
1420
@@ -116,9 +122,9 @@ protected override IQueryable<TEntity> GetQuery(IFetchStrategy<TEntity> fetchStr
116122 /// <summary>
117123 /// Gets an entity query with the given primary key value from the repository.
118124 /// </summary>
119- protected override TEntity GetQuery ( TKey key , IFetchStrategy < TEntity > fetchStrategy )
125+ protected override TEntity GetEntity ( TKey key , IFetchStrategy < TEntity > fetchStrategy )
120126 {
121- return fetchStrategy == null ? DbSet . Find ( key ) : base . GetQuery ( key , fetchStrategy ) ;
127+ return fetchStrategy == null ? DbSet . Find ( key ) : base . GetEntity ( key , fetchStrategy ) ;
122128 }
123129
124130 /// <summary>
@@ -131,5 +137,94 @@ public override void Dispose()
131137 }
132138
133139 #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+ var dbSet = ( DbSet < TEntity > ) DbSet ;
157+
158+ return fetchStrategy == null ? dbSet . FindAsync ( cancellationToken , key ) : base . GetEntityAsync ( key , fetchStrategy , cancellationToken ) ;
159+ }
160+
161+ /// <summary>
162+ /// A protected asynchronous overridable method for getting an entity that satisfies the criteria specified by the <paramref name="criteria" /> from the repository.
163+ /// </summary>
164+ protected override Task < TEntity > GetEntityAsync ( ISpecification < TEntity > criteria , IQueryOptions < TEntity > options , CancellationToken cancellationToken = new CancellationToken ( ) )
165+ {
166+ if ( criteria == null )
167+ throw new ArgumentNullException ( nameof ( criteria ) ) ;
168+
169+ return GetQuery ( criteria , options ) . FirstOrDefaultAsync ( cancellationToken ) ;
170+ }
171+
172+ /// <summary>
173+ /// A protected asynchronous overridable method for getting an entity that satisfies the criteria specified by the <paramref name="criteria" /> from the repository.
174+ /// </summary>
175+ protected override Task < TResult > GetEntityAsync < TResult > ( ISpecification < TEntity > criteria , IQueryOptions < TEntity > options , Expression < Func < TEntity , TResult > > selector , CancellationToken cancellationToken = new CancellationToken ( ) )
176+ {
177+ if ( criteria == null )
178+ throw new ArgumentNullException ( nameof ( criteria ) ) ;
179+
180+ return GetQuery ( criteria , options ) . Select ( selector ) . FirstOrDefaultAsync ( cancellationToken ) ;
181+ }
182+
183+ /// <summary>
184+ /// A protected asynchronous overridable method for getting a collection of entities that satisfies the criteria specified by the <paramref name="criteria" /> from the repository.
185+ /// </summary>
186+ protected override Task < List < TEntity > > GetEntitiesAsync ( ISpecification < TEntity > criteria , IQueryOptions < TEntity > options , CancellationToken cancellationToken = new CancellationToken ( ) )
187+ {
188+ if ( criteria == null )
189+ throw new ArgumentNullException ( nameof ( criteria ) ) ;
190+
191+ return GetQuery ( criteria , options ) . ToListAsync ( cancellationToken ) ;
192+ }
193+
194+ /// <summary>
195+ /// A protected asynchronous overridable method for getting a collection of entities that satisfies the criteria specified by the <paramref name="criteria" /> from the repository.
196+ /// </summary>
197+ protected override Task < List < TResult > > GetEntitiesAsync < TResult > ( ISpecification < TEntity > criteria , IQueryOptions < TEntity > options , Expression < Func < TEntity , TResult > > selector , CancellationToken cancellationToken = new CancellationToken ( ) )
198+ {
199+ if ( criteria == null )
200+ throw new ArgumentNullException ( nameof ( criteria ) ) ;
201+
202+ return GetQuery ( criteria , options ) . Select ( selector ) . ToListAsync ( cancellationToken ) ;
203+ }
204+
205+ /// <summary>
206+ /// 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.
207+ /// </summary>
208+ protected override Task < int > GetCountAsync ( ISpecification < TEntity > criteria , CancellationToken cancellationToken = new CancellationToken ( ) )
209+ {
210+ var predicate = criteria ? . Predicate ;
211+
212+ return predicate == null ? GetQuery ( ) . CountAsync ( cancellationToken ) : GetQuery ( ) . CountAsync ( predicate , cancellationToken ) ;
213+ }
214+
215+ /// <summary>
216+ /// 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.
217+ /// </summary>
218+ protected override Task < bool > GetExistAsync ( ISpecification < TEntity > criteria , CancellationToken cancellationToken = new CancellationToken ( ) )
219+ {
220+ if ( criteria == null )
221+ throw new ArgumentNullException ( nameof ( criteria ) ) ;
222+
223+ var predicate = criteria ? . Predicate ;
224+
225+ return predicate == null ? GetQuery ( ) . AnyAsync ( cancellationToken ) : GetQuery ( ) . AnyAsync ( predicate , cancellationToken ) ;
226+ }
227+
228+ #endregion
134229 }
135230}
0 commit comments