| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- using Abp.Dependency;
- using Abp.Domain.Entities;
- using Abp.Domain.Uow;
- using Abp.MultiTenancy;
- using Abp.Reflection.Extensions;
- namespace Abp.Domain.Repositories
- {
- /// <summary>
- /// Base class to implement <see cref="IRepository{TEntity,TPrimaryKey}"/>.
- /// It implements some methods in most simple way.
- /// </summary>
- /// <typeparam name="TEntity">Type of the Entity for this repository</typeparam>
- /// <typeparam name="TPrimaryKey">Primary key of the entity</typeparam>
- public abstract class AbpRepositoryBase<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey>, IUnitOfWorkManagerAccessor
- where TEntity : class, IEntity<TPrimaryKey>
- {
- /// <summary>
- /// The multi tenancy side
- /// </summary>
- public static MultiTenancySides? MultiTenancySide { get; private set; }
- public IUnitOfWorkManager UnitOfWorkManager { get; set; }
- public IIocResolver IocResolver { get; set; }
- static AbpRepositoryBase()
- {
- var attr = typeof (TEntity).GetSingleAttributeOfTypeOrBaseTypesOrNull<MultiTenancySideAttribute>();
- if (attr != null)
- {
- MultiTenancySide = attr.Side;
- }
- }
- public abstract IQueryable<TEntity> GetAll();
- public virtual IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors)
- {
- return GetAll();
- }
- public virtual List<TEntity> GetAllList()
- {
- return GetAll().ToList();
- }
- public virtual Task<List<TEntity>> GetAllListAsync()
- {
- return Task.FromResult(GetAllList());
- }
-
- public virtual List<TEntity> GetAllList(Expression<Func<TEntity, bool>> predicate)
- {
- return GetAll().Where(predicate).ToList();
- }
- public virtual Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool>> predicate)
- {
- return Task.FromResult(GetAllList(predicate));
- }
- public virtual T Query<T>(Func<IQueryable<TEntity>, T> queryMethod)
- {
- return queryMethod(GetAll());
- }
-
- public virtual TEntity Get(TPrimaryKey id)
- {
- var entity = FirstOrDefault(id);
- if (entity == null)
- {
- throw new EntityNotFoundException(typeof(TEntity), id);
- }
- return entity;
- }
- public virtual async Task<TEntity> GetAsync(TPrimaryKey id)
- {
- var entity = await FirstOrDefaultAsync(id);
- if (entity == null)
- {
- throw new EntityNotFoundException(typeof(TEntity), id);
- }
- return entity;
- }
-
- public virtual TEntity Single(Expression<Func<TEntity, bool>> predicate)
- {
- return GetAll().Single(predicate);
- }
- public virtual Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> predicate)
- {
- return Task.FromResult(Single(predicate));
- }
-
- public virtual TEntity FirstOrDefault(TPrimaryKey id)
- {
- return GetAll().FirstOrDefault(CreateEqualityExpressionForId(id));
- }
- public virtual Task<TEntity> FirstOrDefaultAsync(TPrimaryKey id)
- {
- return Task.FromResult(FirstOrDefault(id));
- }
-
- public virtual TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate)
- {
- return GetAll().FirstOrDefault(predicate);
- }
- public virtual Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate)
- {
- return Task.FromResult(FirstOrDefault(predicate));
- }
-
- public virtual TEntity Load(TPrimaryKey id)
- {
- return Get(id);
- }
- public abstract TEntity Insert(TEntity entity);
-
- public virtual Task<TEntity> InsertAsync(TEntity entity)
- {
- return Task.FromResult(Insert(entity));
- }
- public virtual TPrimaryKey InsertAndGetId(TEntity entity)
- {
- return Insert(entity).Id;
- }
- public virtual Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity)
- {
- return Task.FromResult(InsertAndGetId(entity));
- }
- public virtual TEntity InsertOrUpdate(TEntity entity)
- {
- return entity.IsTransient()
- ? Insert(entity)
- : Update(entity);
- }
-
- public virtual async Task<TEntity> InsertOrUpdateAsync(TEntity entity)
- {
- return entity.IsTransient()
- ? await InsertAsync(entity)
- : await UpdateAsync(entity);
- }
- public virtual TPrimaryKey InsertOrUpdateAndGetId(TEntity entity)
- {
- return InsertOrUpdate(entity).Id;
- }
- public virtual Task<TPrimaryKey> InsertOrUpdateAndGetIdAsync(TEntity entity)
- {
- return Task.FromResult(InsertOrUpdateAndGetId(entity));
- }
- public abstract TEntity Update(TEntity entity);
-
- public virtual Task<TEntity> UpdateAsync(TEntity entity)
- {
- return Task.FromResult(Update(entity));
- }
- public virtual TEntity Update(TPrimaryKey id, Action<TEntity> updateAction)
- {
- var entity = Get(id);
- updateAction(entity);
- return entity;
- }
- public virtual async Task<TEntity> UpdateAsync(TPrimaryKey id, Func<TEntity, Task> updateAction)
- {
- var entity = await GetAsync(id);
- await updateAction(entity);
- return entity;
- }
- public abstract void Delete(TEntity entity);
-
- public virtual Task DeleteAsync(TEntity entity)
- {
- Delete(entity);
- return Task.FromResult(0);
- }
- public abstract void Delete(TPrimaryKey id);
-
- public virtual Task DeleteAsync(TPrimaryKey id)
- {
- Delete(id);
- return Task.FromResult(0);
- }
- public virtual void Delete(Expression<Func<TEntity, bool>> predicate)
- {
- foreach (var entity in GetAll().Where(predicate).ToList())
- {
- Delete(entity);
- }
- }
- public virtual Task DeleteAsync(Expression<Func<TEntity, bool>> predicate)
- {
- Delete(predicate);
- return Task.FromResult(0);
- }
- public virtual int Count()
- {
- return GetAll().Count();
- }
- public virtual Task<int> CountAsync()
- {
- return Task.FromResult(Count());
- }
- public virtual int Count(Expression<Func<TEntity, bool>> predicate)
- {
- return GetAll().Where(predicate).Count();
- }
- public virtual Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate)
- {
- return Task.FromResult(Count(predicate));
- }
- public virtual long LongCount()
- {
- return GetAll().LongCount();
- }
- public virtual Task<long> LongCountAsync()
- {
- return Task.FromResult(LongCount());
- }
- public virtual long LongCount(Expression<Func<TEntity, bool>> predicate)
- {
- return GetAll().Where(predicate).LongCount();
- }
- public virtual Task<long> LongCountAsync(Expression<Func<TEntity, bool>> predicate)
- {
- return Task.FromResult(LongCount(predicate));
- }
- protected virtual Expression<Func<TEntity, bool>> CreateEqualityExpressionForId(TPrimaryKey id)
- {
- var lambdaParam = Expression.Parameter(typeof(TEntity));
- var lambdaBody = Expression.Equal(
- Expression.PropertyOrField(lambdaParam, "Id"),
- Expression.Constant(id, typeof(TPrimaryKey))
- );
- return Expression.Lambda<Func<TEntity, bool>>(lambdaBody, lambdaParam);
- }
- }
- }
|