IRepository enhancements #54

pull/81/head
Halil İbrahim Kalkan 9 years ago
parent ecd75464b2
commit 9af9e7d7fa

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Entities;
@ -40,14 +41,10 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return DbSet.AsQueryable();
}
public override Task<List<TEntity>> GetListAsync()
public override async Task<TEntity> GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken))
{
return GetQueryable().ToListAsync();
}
var entity = await FindAsync(id, cancellationToken);
public override async Task<TEntity> GetAsync(TPrimaryKey id)
{
var entity = await FindAsync(id);
if (entity == null)
{
throw new EntityNotFoundException(typeof(TEntity), id);
@ -56,16 +53,14 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return entity;
}
//TODO: Find by multiple primary key
public override TEntity Find(TPrimaryKey id)
{
return DbSet.Find(id);
}
public override Task<TEntity> FindAsync(TPrimaryKey id)
public override Task<TEntity> FindAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken))
{
return DbSet.FindAsync(id);
return DbSet.FindAsync(new object[] { id }, cancellationToken);
}
public override TEntity Insert(TEntity entity)
@ -80,10 +75,10 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return insertedEntity.Id;
}
public override async Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity)
public override async Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
var insertedEntity = await InsertAsync(entity);
await DbContext.SaveChangesAsync(true);
var insertedEntity = await InsertAsync(entity, cancellationToken);
await DbContext.SaveChangesAsync(cancellationToken);
return insertedEntity.Id;
}
@ -109,9 +104,13 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
DbSet.Remove(entity);
}
public override Task<int> CountAsync()
public override async Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
{
return DbSet.CountAsync();
var entities = await GetQueryable().Where(predicate).ToListAsync(cancellationToken);
foreach (var entity in entities)
{
DbSet.Remove(entity);
}
}
}
}

@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.Domain.Entities;
@ -16,6 +17,8 @@ namespace Volo.Abp.Domain.Repositories
public interface IQueryableRepository<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey>, IQueryable<TEntity>
where TEntity : class, IEntity<TPrimaryKey>
{
//TODO: Is Delete needed?
/// <summary>
/// Deletes many entities by function.
/// Notice that: All entities fits to given predicate are retrieved and deleted.
@ -31,7 +34,8 @@ namespace Volo.Abp.Domain.Repositories
/// This may cause major performance problems if there are too many entities with
/// given predicate.
/// </summary>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <param name="predicate">A condition to filter entities</param>
Task DeleteAsync([NotNull] Expression<Func<TEntity, bool>> predicate);
Task DeleteAsync([NotNull] Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken));
}
}

@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.Domain.Entities;
@ -23,20 +23,6 @@ namespace Volo.Abp.Domain.Repositories
public interface IRepository<TEntity, TPrimaryKey> : IRepository
where TEntity : class, IEntity<TPrimaryKey>
{
/// <summary>
/// Used to get all entities.
/// </summary>
/// <returns>List of all entities</returns>
[NotNull]
List<TEntity> GetList();
/// <summary>
/// Used to get all entities.
/// </summary>
/// <returns>List of all entities</returns>
[NotNull]
Task<List<TEntity>> GetListAsync();
/// <summary>
/// Gets an entity with given primary key.
/// Throws <see cref="EntityNotFoundException"/> if can not find an entity with given id.
@ -51,9 +37,10 @@ namespace Volo.Abp.Domain.Repositories
/// Throws <see cref="EntityNotFoundException"/> if can not find an entity with given id.
/// </summary>
/// <param name="id">Primary key of the entity to get</param>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>Entity</returns>
[NotNull]
Task<TEntity> GetAsync(TPrimaryKey id);
Task<TEntity> GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Gets an entity with given primary key or null if not found.
@ -67,8 +54,9 @@ namespace Volo.Abp.Domain.Repositories
/// Gets an entity with given primary key or null if not found.
/// </summary>
/// <param name="id">Primary key of the entity to get</param>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>Entity or null</returns>
Task<TEntity> FindAsync(TPrimaryKey id);
Task<TEntity> FindAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Inserts a new entity.
@ -80,9 +68,10 @@ namespace Volo.Abp.Domain.Repositories
/// <summary>
/// Inserts a new entity.
/// </summary>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <param name="entity">Inserted entity</param>
[NotNull]
Task<TEntity> InsertAsync([NotNull] TEntity entity);
Task<TEntity> InsertAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Inserts a new entity and gets it's Id.
@ -98,9 +87,10 @@ namespace Volo.Abp.Domain.Repositories
/// It may require to save current unit of work
/// to be able to retrieve id.
/// </summary>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <param name="entity">Entity</param>
/// <returns>Id of the entity</returns>
Task<TPrimaryKey> InsertAndGetIdAsync([NotNull] TEntity entity);
Task<TPrimaryKey> InsertAndGetIdAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Updates an existing entity.
@ -112,9 +102,10 @@ namespace Volo.Abp.Domain.Repositories
/// <summary>
/// Updates an existing entity.
/// </summary>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <param name="entity">Entity</param>
[NotNull]
Task<TEntity> UpdateAsync([NotNull] TEntity entity);
Task<TEntity> UpdateAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Deletes an entity.
@ -125,8 +116,9 @@ namespace Volo.Abp.Domain.Repositories
/// <summary>
/// Deletes an entity.
/// </summary>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <param name="entity">Entity to be deleted</param>
Task DeleteAsync([NotNull] TEntity entity);
Task DeleteAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Deletes an entity by primary key.
@ -137,19 +129,8 @@ namespace Volo.Abp.Domain.Repositories
/// <summary>
/// Deletes an entity by primary key.
/// </summary>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <param name="id">Primary key of the entity</param>
Task DeleteAsync(TPrimaryKey id);
/// <summary>
/// Gets count of all entities in this repository.
/// </summary>
/// <returns>Count of entities</returns>
int Count();
/// <summary>
/// Gets count of all entities in this repository.
/// </summary>
/// <returns>Count of entities</returns>
Task<int> CountAsync();
Task DeleteAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken));
}
}

@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities;
@ -35,11 +36,6 @@ namespace Volo.Abp.Domain.Repositories
protected abstract IQueryable<TEntity> GetQueryable();
public override List<TEntity> GetList()
{
return GetQueryable().ToList();
}
public override TEntity Find(TPrimaryKey id)
{
return GetQueryable().FirstOrDefault(CreateEqualityExpressionForId(id));
@ -53,15 +49,10 @@ namespace Volo.Abp.Domain.Repositories
}
}
public virtual Task DeleteAsync(Expression<Func<TEntity, bool>> predicate)
public virtual Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
{
Delete(predicate);
return Task.CompletedTask;
}
public override int Count()
{
return GetQueryable().Count();
}
}
}

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities;
@ -15,16 +15,10 @@ namespace Volo.Abp.Domain.Repositories
public abstract class RepositoryBase<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
public abstract List<TEntity> GetList();
public virtual Task<List<TEntity>> GetListAsync()
{
return Task.FromResult(GetList());
}
public virtual TEntity Get(TPrimaryKey id)
{
var entity = Find(id);
if (entity == null)
{
throw new EntityNotFoundException(typeof(TEntity), id);
@ -33,21 +27,21 @@ namespace Volo.Abp.Domain.Repositories
return entity;
}
public virtual Task<TEntity> GetAsync(TPrimaryKey id)
public virtual Task<TEntity> GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(Get(id));
}
public abstract TEntity Find(TPrimaryKey id);
public virtual Task<TEntity> FindAsync(TPrimaryKey id)
public virtual Task<TEntity> FindAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(Find(id));
}
public abstract TEntity Insert(TEntity entity);
public virtual Task<TEntity> InsertAsync(TEntity entity)
public virtual Task<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(Insert(entity));
}
@ -57,21 +51,21 @@ namespace Volo.Abp.Domain.Repositories
return Insert(entity).Id;
}
public virtual Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity)
public virtual Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(InsertAndGetId(entity));
}
public abstract TEntity Update(TEntity entity);
public virtual Task<TEntity> UpdateAsync(TEntity entity)
public virtual Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(Update(entity));
}
public abstract void Delete(TEntity entity);
public virtual Task DeleteAsync(TEntity entity)
public virtual Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
Delete(entity);
return Task.CompletedTask;
@ -88,19 +82,12 @@ namespace Volo.Abp.Domain.Repositories
Delete(entity);
}
public virtual Task DeleteAsync(TPrimaryKey id)
public virtual Task DeleteAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken))
{
Delete(id);
return Task.CompletedTask;
}
public abstract int Count();
public virtual Task<int> CountAsync()
{
return Task.FromResult(Count());
}
protected static Expression<Func<TEntity, bool>> CreateEqualityExpressionForId(TPrimaryKey id)
{
var lambdaParam = Expression.Parameter(typeof(TEntity));

Loading…
Cancel
Save