Skip to content

Commit f6a5a89

Browse files
Merge pull request #11 from johelvisguzman/dev
Merge changes for v1.2.0 release
2 parents 86c44ae + 4ec6b61 commit f6a5a89

File tree

19 files changed

+1598
-95
lines changed

19 files changed

+1598
-95
lines changed

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pull_requests:
1111

1212
branches:
1313
only:
14+
- dev
1415
- master
1516

1617
# Do not build on tags (GitHub only)

src/DotNetToolkit.Repository.EntityFramework/EfRepositoryBase.cs

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
namespace 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
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace DotNetToolkit.Repository
2+
{
3+
using System;
4+
using Traits;
5+
6+
/// <summary>
7+
/// Represents an asynchronous repository.
8+
/// </summary>
9+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
10+
/// <typeparam name="TKey">The type of the primary key.</typeparam>
11+
public interface IRepositoryAsync<TEntity, in TKey> : IRepository<TEntity, TKey>, ICanAggregateAsync<TEntity>, ICanAddAsync<TEntity>, ICanUpdateAsync<TEntity>, ICanDeleteAsync<TEntity, TKey>, ICanGetAsync<TEntity, TKey>, ICanFindAsync<TEntity>, IDisposable
12+
where TEntity : class
13+
{
14+
}
15+
16+
/// <summary>
17+
/// Represents an asynchronous repository with a default primary key value of type integer.
18+
/// </summary>
19+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
20+
public interface IRepositoryAsync<TEntity> : IRepositoryAsync<TEntity, int> where TEntity : class
21+
{
22+
}
23+
}

src/DotNetToolkit.Repository/Properties/Resources.Designer.cs

Lines changed: 27 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DotNetToolkit.Repository/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="EntityKeyNotFound" xml:space="preserve">
121+
<value>No entity found in the repository with the '{0}' key.</value>
122+
</data>
120123
<data name="EntityRequiresPrimaryKey" xml:space="preserve">
121124
<value>The instance of entity type '{0}' requires a primary key to be defined.</value>
122125
</data>

0 commit comments

Comments
 (0)