Browse Source

IRepository enhancements #54

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
9af9e7d7fa
  1. 31
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  2. 6
      src/Volo.Abp/Volo/Abp/Domain/Repositories/IQueryableRepository.cs
  3. 49
      src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs
  4. 13
      src/Volo.Abp/Volo/Abp/Domain/Repositories/QueryableRepositoryBase.cs
  5. 31
      src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryBase.cs

31
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
@ -40,14 +41,10 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return DbSet.AsQueryable(); 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) if (entity == null)
{ {
throw new EntityNotFoundException(typeof(TEntity), id); throw new EntityNotFoundException(typeof(TEntity), id);
@ -56,16 +53,14 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return entity; return entity;
} }
//TODO: Find by multiple primary key
public override TEntity Find(TPrimaryKey id) public override TEntity Find(TPrimaryKey id)
{ {
return DbSet.Find(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) public override TEntity Insert(TEntity entity)
@ -80,10 +75,10 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return insertedEntity.Id; 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); var insertedEntity = await InsertAsync(entity, cancellationToken);
await DbContext.SaveChangesAsync(true); await DbContext.SaveChangesAsync(cancellationToken);
return insertedEntity.Id; return insertedEntity.Id;
} }
@ -109,9 +104,13 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
DbSet.Remove(entity); 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);
}
} }
} }
} }

6
src/Volo.Abp/Volo/Abp/Domain/Repositories/IQueryableRepository.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
@ -16,6 +17,8 @@ namespace Volo.Abp.Domain.Repositories
public interface IQueryableRepository<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey>, IQueryable<TEntity> public interface IQueryableRepository<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey>, IQueryable<TEntity>
where TEntity : class, IEntity<TPrimaryKey> where TEntity : class, IEntity<TPrimaryKey>
{ {
//TODO: Is Delete needed?
/// <summary> /// <summary>
/// Deletes many entities by function. /// Deletes many entities by function.
/// Notice that: All entities fits to given predicate are retrieved and deleted. /// 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 /// This may cause major performance problems if there are too many entities with
/// given predicate. /// given predicate.
/// </summary> /// </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> /// <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));
} }
} }

49
src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs

@ -1,4 +1,4 @@
using System.Collections.Generic; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
@ -23,20 +23,6 @@ namespace Volo.Abp.Domain.Repositories
public interface IRepository<TEntity, TPrimaryKey> : IRepository public interface IRepository<TEntity, TPrimaryKey> : IRepository
where TEntity : class, IEntity<TPrimaryKey> 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> /// <summary>
/// Gets an entity with given primary key. /// Gets an entity with given primary key.
/// Throws <see cref="EntityNotFoundException"/> if can not find an entity with given id. /// 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. /// Throws <see cref="EntityNotFoundException"/> if can not find an entity with given id.
/// </summary> /// </summary>
/// <param name="id">Primary key of the entity to get</param> /// <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> /// <returns>Entity</returns>
[NotNull] [NotNull]
Task<TEntity> GetAsync(TPrimaryKey id); Task<TEntity> GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken));
/// <summary> /// <summary>
/// Gets an entity with given primary key or null if not found. /// 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. /// Gets an entity with given primary key or null if not found.
/// </summary> /// </summary>
/// <param name="id">Primary key of the entity to get</param> /// <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> /// <returns>Entity or null</returns>
Task<TEntity> FindAsync(TPrimaryKey id); Task<TEntity> FindAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken));
/// <summary> /// <summary>
/// Inserts a new entity. /// Inserts a new entity.
@ -80,9 +68,10 @@ namespace Volo.Abp.Domain.Repositories
/// <summary> /// <summary>
/// Inserts a new entity. /// Inserts a new entity.
/// </summary> /// </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> /// <param name="entity">Inserted entity</param>
[NotNull] [NotNull]
Task<TEntity> InsertAsync([NotNull] TEntity entity); Task<TEntity> InsertAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
/// <summary> /// <summary>
/// Inserts a new entity and gets it's Id. /// 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 /// It may require to save current unit of work
/// to be able to retrieve id. /// to be able to retrieve id.
/// </summary> /// </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> /// <param name="entity">Entity</param>
/// <returns>Id of the entity</returns> /// <returns>Id of the entity</returns>
Task<TPrimaryKey> InsertAndGetIdAsync([NotNull] TEntity entity); Task<TPrimaryKey> InsertAndGetIdAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
/// <summary> /// <summary>
/// Updates an existing entity. /// Updates an existing entity.
@ -112,9 +102,10 @@ namespace Volo.Abp.Domain.Repositories
/// <summary> /// <summary>
/// Updates an existing entity. /// Updates an existing entity.
/// </summary> /// </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> /// <param name="entity">Entity</param>
[NotNull] [NotNull]
Task<TEntity> UpdateAsync([NotNull] TEntity entity); Task<TEntity> UpdateAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
/// <summary> /// <summary>
/// Deletes an entity. /// Deletes an entity.
@ -125,8 +116,9 @@ namespace Volo.Abp.Domain.Repositories
/// <summary> /// <summary>
/// Deletes an entity. /// Deletes an entity.
/// </summary> /// </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> /// <param name="entity">Entity to be deleted</param>
Task DeleteAsync([NotNull] TEntity entity); Task DeleteAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
/// <summary> /// <summary>
/// Deletes an entity by primary key. /// Deletes an entity by primary key.
@ -137,19 +129,8 @@ namespace Volo.Abp.Domain.Repositories
/// <summary> /// <summary>
/// Deletes an entity by primary key. /// Deletes an entity by primary key.
/// </summary> /// </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> /// <param name="id">Primary key of the entity</param>
Task DeleteAsync(TPrimaryKey id); Task DeleteAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken));
/// <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();
} }
} }

13
src/Volo.Abp/Volo/Abp/Domain/Repositories/QueryableRepositoryBase.cs

@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
@ -35,11 +36,6 @@ namespace Volo.Abp.Domain.Repositories
protected abstract IQueryable<TEntity> GetQueryable(); protected abstract IQueryable<TEntity> GetQueryable();
public override List<TEntity> GetList()
{
return GetQueryable().ToList();
}
public override TEntity Find(TPrimaryKey id) public override TEntity Find(TPrimaryKey id)
{ {
return GetQueryable().FirstOrDefault(CreateEqualityExpressionForId(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); Delete(predicate);
return Task.CompletedTask; return Task.CompletedTask;
} }
public override int Count()
{
return GetQueryable().Count();
}
} }
} }

31
src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryBase.cs

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

Loading…
Cancel
Save