saritasa-nest / saritasa-dotnet-tools

Development Tools For Company'S .NET Projects.
BSD 2-Clause "Simplified" License
27 stars 13 forks source link

Implement async repositories #36

Closed dermeister0 closed 7 years ago

dermeister0 commented 7 years ago

It should have GetAsync, GetAllAsync, FindAsync, Add, AddRange, Remove, RemoveRange methods.

For example:

    public class EFAsyncRepository<TEntity, TContext> : IAsyncRepository<TEntity>
        where TEntity : class
        where TContext : DbContext
    {
        protected TContext Context;

        public EFAsyncRepository(TContext context)
        {
            Context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public async Task<IEnumerable<TEntity>> GetAllAsync()
        {
            return await Context.Set<TEntity>().ToArrayAsync();
        }
    }
krasninja commented 7 years ago

Take a look:

dermeister0 commented 7 years ago

It's not possible to use IAsyncRepository without IRepository, because it does not have Add method (only AddAsync).

Methods AddAsync, AddRangeAsync, RemoveAsync and RemoveRangeAsync are useless. Proposed interface:

public interface IAsyncRepository
{
    void Add(TEntity entity);
    void AddRange(IEnumerable<TEntity> entities);
    Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken));
    Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate, IEnumerable<Expression<Func<TEntity, object>>> includes, CancellationToken cancellationToken = default(CancellationToken));
    Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default(CancellationToken));
    Task<IEnumerable<TEntity>> GetAllAsync(IEnumerable<Expression<Func<TEntity, object>>> includes, CancellationToken cancellationToken = default(CancellationToken));
    Task<TEntity> GetAsync(params object[] keyValues, CancellationToken cancellationToken = default(CancellationToken));
    void Remove(TEntity entity);
    void RemoveRange(IEnumerable<TEntity> entities);
}
krasninja commented 7 years ago

Please take a look at new version.