mirror of https://github.com/abpframework/abp.git
49 changed files with 380 additions and 380 deletions
Binary file not shown.
@ -0,0 +1,90 @@ |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.Domain.Repositories |
|||
{ |
|||
public abstract class BasicRepositoryBase<TEntity> : IBasicRepository<TEntity> |
|||
where TEntity : class, IEntity |
|||
{ |
|||
public ICancellationTokenProvider CancellationTokenProvider { get; set; } |
|||
|
|||
protected BasicRepositoryBase() |
|||
{ |
|||
CancellationTokenProvider = NullCancellationTokenProvider.Instance; |
|||
} |
|||
|
|||
public abstract TEntity Insert(TEntity entity, bool autoSave = false); |
|||
|
|||
public virtual Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) |
|||
{ |
|||
return Task.FromResult(Insert(entity, autoSave)); |
|||
} |
|||
|
|||
public abstract TEntity Update(TEntity entity); |
|||
|
|||
public virtual Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default) |
|||
{ |
|||
return Task.FromResult(Update(entity)); |
|||
} |
|||
|
|||
public abstract void Delete(TEntity entity); |
|||
|
|||
public virtual Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default) |
|||
{ |
|||
Delete(entity); |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
protected virtual CancellationToken GetCancellationToken(CancellationToken prefferedValue = default) |
|||
{ |
|||
return CancellationTokenProvider.FallbackToProvider(prefferedValue); |
|||
} |
|||
} |
|||
|
|||
public abstract class BasicRepositoryBase<TEntity, TKey> : BasicRepositoryBase<TEntity>, IBasicRepository<TEntity, TKey> |
|||
where TEntity : class, IEntity<TKey> |
|||
{ |
|||
public virtual TEntity Get(TKey id) |
|||
{ |
|||
var entity = Find(id); |
|||
|
|||
if (entity == null) |
|||
{ |
|||
throw new EntityNotFoundException(typeof(TEntity), id); |
|||
} |
|||
|
|||
return entity; |
|||
} |
|||
|
|||
public virtual Task<TEntity> GetAsync(TKey id, CancellationToken cancellationToken = default) |
|||
{ |
|||
return Task.FromResult(Get(id)); |
|||
} |
|||
|
|||
public abstract TEntity Find(TKey id); |
|||
|
|||
public virtual Task<TEntity> FindAsync(TKey id, CancellationToken cancellationToken = default) |
|||
{ |
|||
return Task.FromResult(Find(id)); |
|||
} |
|||
|
|||
public virtual void Delete(TKey id) |
|||
{ |
|||
var entity = Find(id); |
|||
if (entity == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
Delete(entity); |
|||
} |
|||
|
|||
public virtual Task DeleteAsync(TKey id, CancellationToken cancellationToken = default) |
|||
{ |
|||
Delete(id); |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,114 @@ |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.Domain.Repositories |
|||
{ |
|||
public interface IBasicRepository<TEntity> : IRepository |
|||
where TEntity : class, IEntity |
|||
{ |
|||
/// <summary>
|
|||
/// Inserts a new entity.
|
|||
/// </summary>
|
|||
/// <param name="entity">Inserted entity</param>
|
|||
/// <param name="autoSave">
|
|||
/// Set true to automatically save changes to database.
|
|||
/// This can be used to set database generated Id of an entity for some ORMs (like Entity Framework).
|
|||
/// </param>
|
|||
[NotNull] |
|||
TEntity Insert([NotNull] TEntity entity, bool autoSave = false); |
|||
|
|||
/// <summary>
|
|||
/// Inserts a new entity.
|
|||
/// </summary>
|
|||
/// <param name="autoSave">
|
|||
/// Set true to automatically save changes to database.
|
|||
/// This can be used to set database generated Id of an entity for some ORMs (like Entity Framework).
|
|||
/// </param>
|
|||
/// <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, bool autoSave = false, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// Updates an existing entity.
|
|||
/// </summary>
|
|||
/// <param name="entity">Entity</param>
|
|||
[NotNull] |
|||
TEntity Update([NotNull] TEntity entity); |
|||
|
|||
/// <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, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// Deletes an entity.
|
|||
/// </summary>
|
|||
/// <param name="entity">Entity to be deleted</param>
|
|||
void Delete([NotNull] TEntity entity); //TODO: Return true if deleted
|
|||
|
|||
/// <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, CancellationToken cancellationToken = default); //TODO: Return true if deleted
|
|||
} |
|||
|
|||
public interface IBasicRepository<TEntity, TKey> : IBasicRepository<TEntity> |
|||
where TEntity : class, IEntity<TKey> |
|||
{ |
|||
/// <summary>
|
|||
/// Gets an entity with given primary key.
|
|||
/// 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>
|
|||
/// <returns>Entity</returns>
|
|||
[NotNull] |
|||
TEntity Get(TKey id); |
|||
|
|||
/// <summary>
|
|||
/// Gets an entity with given primary key.
|
|||
/// 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(TKey id, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// Gets an entity with given primary key or null if not found.
|
|||
/// </summary>
|
|||
/// <param name="id">Primary key of the entity to get</param>
|
|||
/// <returns>Entity or null</returns>
|
|||
[CanBeNull] |
|||
TEntity Find(TKey id); |
|||
|
|||
/// <summary>
|
|||
/// 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(TKey id, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// Deletes an entity by primary key.
|
|||
/// </summary>
|
|||
/// <param name="id">Primary key of the entity</param>
|
|||
void Delete(TKey id); //TODO: Return true if deleted
|
|||
|
|||
/// <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(TKey id, CancellationToken cancellationToken = default); //TODO: Return true if deleted
|
|||
} |
|||
} |
|||
@ -1,38 +0,0 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Linq.Expressions; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.Domain.Repositories |
|||
{ |
|||
public interface IQueryableRepository<TEntity> : IRepository<TEntity>, IQueryable<TEntity> |
|||
where TEntity : class, IEntity |
|||
{ |
|||
/// <summary>
|
|||
/// Deletes many entities by function.
|
|||
/// Notice that: All entities fits to given predicate are retrieved and deleted.
|
|||
/// This may cause major performance problems if there are too many entities with
|
|||
/// given predicate.
|
|||
/// </summary>
|
|||
/// <param name="predicate">A condition to filter entities</param>
|
|||
void Delete([NotNull] Expression<Func<TEntity, bool>> predicate); |
|||
|
|||
/// <summary>
|
|||
/// Deletes many entities by function.
|
|||
/// Notice that: All entities fits to given predicate are retrieved and deleted.
|
|||
/// 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, CancellationToken cancellationToken = default); |
|||
} |
|||
|
|||
public interface IQueryableRepository<TEntity, TKey> : IQueryableRepository<TEntity>, IRepository<TEntity, TKey> |
|||
where TEntity : class, IEntity<TKey> |
|||
{ |
|||
} |
|||
} |
|||
@ -1,118 +0,0 @@ |
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Linq.Expressions; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Volo.Abp.Domain.Repositories |
|||
{ |
|||
public abstract class QueryableRepositoryBase<TEntity> : RepositoryBase<TEntity>, IQueryableRepository<TEntity> |
|||
where TEntity : class, IEntity |
|||
{ |
|||
public IDataFilter DataFilter { get; set; } |
|||
|
|||
public ICurrentTenant CurrentTenant { get; set; } |
|||
|
|||
public virtual Type ElementType => GetQueryable().ElementType; |
|||
|
|||
public virtual Expression Expression => GetQueryable().Expression; |
|||
|
|||
public virtual IQueryProvider Provider => GetQueryable().Provider; |
|||
|
|||
IEnumerator IEnumerable.GetEnumerator() |
|||
{ |
|||
return GetEnumerator(); |
|||
} |
|||
|
|||
public IEnumerator<TEntity> GetEnumerator() |
|||
{ |
|||
return GetQueryable().GetEnumerator(); |
|||
} |
|||
|
|||
protected abstract IQueryable<TEntity> GetQueryable(); |
|||
|
|||
public virtual void Delete(Expression<Func<TEntity, bool>> predicate) |
|||
{ |
|||
foreach (var entity in GetQueryable().Where(predicate).ToList()) |
|||
{ |
|||
Delete(entity); |
|||
} |
|||
} |
|||
|
|||
public virtual Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) |
|||
{ |
|||
Delete(predicate); |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
//TODO: Is that needed..?
|
|||
protected virtual IQueryable<TEntity> ApplyDataFilters(IQueryable<TEntity> query) |
|||
{ |
|||
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity))) |
|||
{ |
|||
query = query.WhereIf(DataFilter.IsEnabled<ISoftDelete>(), e => ((ISoftDelete)e).IsDeleted == false); |
|||
} |
|||
|
|||
if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity))) |
|||
{ |
|||
var tenantId = CurrentTenant.Id; |
|||
query = query.WhereIf(DataFilter.IsEnabled<IMultiTenant>(), e => ((IMultiTenant)e).TenantId == tenantId); |
|||
} |
|||
|
|||
return query; |
|||
} |
|||
} |
|||
|
|||
public abstract class QueryableRepositoryBase<TEntity, TKey> : QueryableRepositoryBase<TEntity>, IQueryableRepository<TEntity, TKey> |
|||
where TEntity : class, IEntity<TKey> |
|||
{ |
|||
public virtual TEntity Find(TKey id) |
|||
{ |
|||
return GetQueryable().FirstOrDefault(EntityHelper.CreateEqualityExpressionForId<TEntity, TKey>(id)); |
|||
} |
|||
|
|||
public virtual TEntity Get(TKey id) |
|||
{ |
|||
var entity = Find(id); |
|||
|
|||
if (entity == null) |
|||
{ |
|||
throw new EntityNotFoundException(typeof(TEntity), id); |
|||
} |
|||
|
|||
return entity; |
|||
} |
|||
|
|||
public virtual Task<TEntity> GetAsync(TKey id, CancellationToken cancellationToken = default) |
|||
{ |
|||
return Task.FromResult(Get(id)); |
|||
} |
|||
|
|||
public virtual Task<TEntity> FindAsync(TKey id, CancellationToken cancellationToken = default) |
|||
{ |
|||
return Task.FromResult(Find(id)); |
|||
} |
|||
|
|||
public virtual void Delete(TKey id) |
|||
{ |
|||
var entity = Find(id); |
|||
if (entity == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
Delete(entity); |
|||
} |
|||
|
|||
public virtual Task DeleteAsync(TKey id, CancellationToken cancellationToken = default) |
|||
{ |
|||
Delete(id); |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue