mirror of https://github.com/abpframework/abp.git
65 changed files with 576 additions and 937 deletions
@ -1,13 +0,0 @@ |
|||
using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Client |
|||
{ |
|||
public static class CachedApplicationConfigurationClientExtensions |
|||
{ |
|||
public static ApplicationConfigurationDto Get(this ICachedApplicationConfigurationClient client) |
|||
{ |
|||
return AsyncHelper.RunSync(client.GetAsync); |
|||
} |
|||
} |
|||
} |
|||
@ -1,168 +0,0 @@ |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Application.Dtos; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.Linq; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Volo.Abp.Application.Services |
|||
{ |
|||
public abstract class AsyncCrudAppService<TEntity, TEntityDto, TKey> |
|||
: AsyncCrudAppService<TEntity, TEntityDto, TKey, PagedAndSortedResultRequestDto> |
|||
where TEntity : class, IEntity<TKey> |
|||
where TEntityDto : IEntityDto<TKey> |
|||
{ |
|||
protected AsyncCrudAppService(IRepository<TEntity, TKey> repository) |
|||
: base(repository) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
|
|||
public abstract class AsyncCrudAppService<TEntity, TEntityDto, TKey, TGetListInput> |
|||
: AsyncCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TEntityDto, TEntityDto> |
|||
where TEntity : class, IEntity<TKey> |
|||
where TEntityDto : IEntityDto<TKey> |
|||
{ |
|||
protected AsyncCrudAppService(IRepository<TEntity, TKey> repository) |
|||
: base(repository) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
|
|||
public abstract class AsyncCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput> |
|||
: AsyncCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput> |
|||
where TEntity : class, IEntity<TKey> |
|||
where TEntityDto : IEntityDto<TKey> |
|||
{ |
|||
protected AsyncCrudAppService(IRepository<TEntity, TKey> repository) |
|||
: base(repository) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
|
|||
public abstract class AsyncCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput> |
|||
: AsyncCrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput> |
|||
where TEntity : class, IEntity<TKey> |
|||
where TEntityDto : IEntityDto<TKey> |
|||
{ |
|||
protected AsyncCrudAppService(IRepository<TEntity, TKey> repository) |
|||
: base(repository) |
|||
{ |
|||
|
|||
} |
|||
|
|||
protected override TEntityDto MapToGetListOutputDto(TEntity entity) |
|||
{ |
|||
return MapToGetOutputDto(entity); |
|||
} |
|||
} |
|||
|
|||
public abstract class AsyncCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput> |
|||
: CrudAppServiceBase<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>, |
|||
IAsyncCrudAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput> |
|||
where TEntity : class, IEntity<TKey> |
|||
where TGetOutputDto : IEntityDto<TKey> |
|||
where TGetListOutputDto : IEntityDto<TKey> |
|||
{ |
|||
public IAsyncQueryableExecuter AsyncQueryableExecuter { get; set; } |
|||
|
|||
protected AsyncCrudAppService(IRepository<TEntity, TKey> repository) |
|||
: base(repository) |
|||
{ |
|||
AsyncQueryableExecuter = DefaultAsyncQueryableExecuter.Instance; |
|||
} |
|||
|
|||
public virtual async Task<TGetOutputDto> GetAsync(TKey id) |
|||
{ |
|||
await CheckGetPolicyAsync(); |
|||
|
|||
var entity = await GetEntityByIdAsync(id); |
|||
return MapToGetOutputDto(entity); |
|||
} |
|||
|
|||
public virtual async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input) |
|||
{ |
|||
await CheckGetListPolicyAsync(); |
|||
|
|||
var query = CreateFilteredQuery(input); |
|||
|
|||
var totalCount = await AsyncQueryableExecuter.CountAsync(query); |
|||
|
|||
query = ApplySorting(query, input); |
|||
query = ApplyPaging(query, input); |
|||
|
|||
var entities = await AsyncQueryableExecuter.ToListAsync(query); |
|||
|
|||
return new PagedResultDto<TGetListOutputDto>( |
|||
totalCount, |
|||
entities.Select(MapToGetListOutputDto).ToList() |
|||
); |
|||
} |
|||
|
|||
public virtual async Task<TGetOutputDto> CreateAsync(TCreateInput input) |
|||
{ |
|||
await CheckCreatePolicyAsync(); |
|||
|
|||
var entity = MapToEntity(input); |
|||
|
|||
TryToSetTenantId(entity); |
|||
|
|||
await Repository.InsertAsync(entity, autoSave: true); |
|||
|
|||
return MapToGetOutputDto(entity); |
|||
} |
|||
|
|||
public virtual async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input) |
|||
{ |
|||
await CheckUpdatePolicyAsync(); |
|||
|
|||
var entity = await GetEntityByIdAsync(id); |
|||
//TODO: Check if input has id different than given id and normalize if it's default value, throw ex otherwise
|
|||
MapToEntity(input, entity); |
|||
await Repository.UpdateAsync(entity, autoSave: true); |
|||
|
|||
return MapToGetOutputDto(entity); |
|||
} |
|||
|
|||
public virtual async Task DeleteAsync(TKey id) |
|||
{ |
|||
await CheckDeletePolicyAsync(); |
|||
|
|||
await Repository.DeleteAsync(id); |
|||
} |
|||
|
|||
protected virtual Task<TEntity> GetEntityByIdAsync(TKey id) |
|||
{ |
|||
return Repository.GetAsync(id); |
|||
} |
|||
|
|||
protected virtual async Task CheckGetPolicyAsync() |
|||
{ |
|||
await CheckPolicyAsync(GetPolicyName); |
|||
} |
|||
|
|||
protected virtual async Task CheckGetListPolicyAsync() |
|||
{ |
|||
await CheckPolicyAsync(GetListPolicyName); |
|||
} |
|||
|
|||
protected virtual async Task CheckCreatePolicyAsync() |
|||
{ |
|||
await CheckPolicyAsync(CreatePolicyName); |
|||
} |
|||
|
|||
protected virtual async Task CheckUpdatePolicyAsync() |
|||
{ |
|||
await CheckPolicyAsync(UpdatePolicyName); |
|||
} |
|||
|
|||
protected virtual async Task CheckDeletePolicyAsync() |
|||
{ |
|||
await CheckPolicyAsync(DeletePolicyName); |
|||
} |
|||
} |
|||
} |
|||
@ -1,191 +0,0 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Linq.Dynamic.Core; |
|||
using Volo.Abp.Application.Dtos; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.Abp.ObjectMapping; |
|||
|
|||
namespace Volo.Abp.Application.Services |
|||
{ |
|||
/// <summary>
|
|||
/// This is a common base class for CrudAppService and AsyncCrudAppService classes.
|
|||
/// Inherit either from CrudAppService or AsyncCrudAppService, not from this class.
|
|||
/// </summary>
|
|||
public abstract class CrudAppServiceBase<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput> : |
|||
ApplicationService |
|||
where TEntity : class, IEntity<TKey> |
|||
where TGetOutputDto : IEntityDto<TKey> |
|||
where TGetListOutputDto : IEntityDto<TKey> |
|||
{ |
|||
protected IRepository<TEntity, TKey> Repository { get; } |
|||
|
|||
protected virtual string GetPolicyName { get; set; } |
|||
|
|||
protected virtual string GetListPolicyName { get; set; } |
|||
|
|||
protected virtual string CreatePolicyName { get; set; } |
|||
|
|||
protected virtual string UpdatePolicyName { get; set; } |
|||
|
|||
protected virtual string DeletePolicyName { get; set; } |
|||
|
|||
protected CrudAppServiceBase(IRepository<TEntity, TKey> repository) |
|||
{ |
|||
Repository = repository; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Should apply sorting if needed.
|
|||
/// </summary>
|
|||
/// <param name="query">The query.</param>
|
|||
/// <param name="input">The input.</param>
|
|||
protected virtual IQueryable<TEntity> ApplySorting(IQueryable<TEntity> query, TGetListInput input) |
|||
{ |
|||
//Try to sort query if available
|
|||
var sortInput = input as ISortedResultRequest; |
|||
if (sortInput != null) |
|||
{ |
|||
if (!sortInput.Sorting.IsNullOrWhiteSpace()) |
|||
{ |
|||
return query.OrderBy(sortInput.Sorting); |
|||
} |
|||
} |
|||
|
|||
//IQueryable.Task requires sorting, so we should sort if Take will be used.
|
|||
if (input is ILimitedResultRequest) |
|||
{ |
|||
return query.OrderByDescending(e => e.Id); |
|||
} |
|||
|
|||
//No sorting
|
|||
return query; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Should apply paging if needed.
|
|||
/// </summary>
|
|||
/// <param name="query">The query.</param>
|
|||
/// <param name="input">The input.</param>
|
|||
protected virtual IQueryable<TEntity> ApplyPaging(IQueryable<TEntity> query, TGetListInput input) |
|||
{ |
|||
//Try to use paging if available
|
|||
var pagedInput = input as IPagedResultRequest; |
|||
if (pagedInput != null) |
|||
{ |
|||
return query.PageBy(pagedInput); |
|||
} |
|||
|
|||
//Try to limit query result if available
|
|||
var limitedInput = input as ILimitedResultRequest; |
|||
if (limitedInput != null) |
|||
{ |
|||
return query.Take(limitedInput.MaxResultCount); |
|||
} |
|||
|
|||
//No paging
|
|||
return query; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// This method should create <see cref="IQueryable{TEntity}"/> based on given input.
|
|||
/// It should filter query if needed, but should not do sorting or paging.
|
|||
/// Sorting should be done in <see cref="ApplySorting"/> and paging should be done in <see cref="ApplyPaging"/>
|
|||
/// methods.
|
|||
/// </summary>
|
|||
/// <param name="input">The input.</param>
|
|||
protected virtual IQueryable<TEntity> CreateFilteredQuery(TGetListInput input) |
|||
{ |
|||
return Repository; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TEntity"/> to <see cref="TGetOutputDto"/>.
|
|||
/// It uses <see cref="IObjectMapper"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// </summary>
|
|||
protected virtual TGetOutputDto MapToGetOutputDto(TEntity entity) |
|||
{ |
|||
return ObjectMapper.Map<TEntity, TGetOutputDto>(entity); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TEntity"/> to <see cref="TGetListOutputDto"/>.
|
|||
/// It uses <see cref="IObjectMapper"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// </summary>
|
|||
protected virtual TGetListOutputDto MapToGetListOutputDto(TEntity entity) |
|||
{ |
|||
return ObjectMapper.Map<TEntity, TGetListOutputDto>(entity); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TCreateInput"/> to <see cref="TEntity"/> to create a new entity.
|
|||
/// It uses <see cref="IObjectMapper"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// </summary>
|
|||
protected virtual TEntity MapToEntity(TCreateInput createInput) |
|||
{ |
|||
var entity = ObjectMapper.Map<TCreateInput, TEntity>(createInput); |
|||
SetIdForGuids(entity); |
|||
return entity; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets Id value for the entity if <see cref="TKey"/> is <see cref="Guid"/>.
|
|||
/// It's used while creating a new entity.
|
|||
/// </summary>
|
|||
protected virtual void SetIdForGuids(TEntity entity) |
|||
{ |
|||
var entityWithGuidId = entity as IEntity<Guid>; |
|||
|
|||
if (entityWithGuidId == null || entityWithGuidId.Id != Guid.Empty) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
entityWithGuidId.Id = GuidGenerator.Create(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Maps <see cref="TUpdateInput"/> to <see cref="TEntity"/> to update the entity.
|
|||
/// It uses <see cref="IObjectMapper"/> by default.
|
|||
/// It can be overriden for custom mapping.
|
|||
/// </summary>
|
|||
protected virtual void MapToEntity(TUpdateInput updateInput, TEntity entity) |
|||
{ |
|||
if (updateInput is IEntityDto<TKey> entityDto) |
|||
{ |
|||
entityDto.Id = entity.Id; |
|||
} |
|||
|
|||
ObjectMapper.Map(updateInput, entity); |
|||
} |
|||
|
|||
protected virtual void TryToSetTenantId(TEntity entity) |
|||
{ |
|||
if (entity is IMultiTenant && HasTenantIdProperty(entity)) |
|||
{ |
|||
var tenantId = CurrentTenant.Id; |
|||
|
|||
if (!tenantId.HasValue) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var propertyInfo = entity.GetType().GetProperty(nameof(IMultiTenant.TenantId)); |
|||
|
|||
if (propertyInfo != null && propertyInfo.GetSetMethod() != null) |
|||
{ |
|||
propertyInfo.SetValue(entity, tenantId, null); |
|||
} |
|||
} |
|||
} |
|||
|
|||
protected virtual bool HasTenantIdProperty(TEntity entity) |
|||
{ |
|||
return entity.GetType().GetProperty(nameof(IMultiTenant.TenantId)) != null; |
|||
} |
|||
} |
|||
} |
|||
@ -1,49 +0,0 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Application.Dtos; |
|||
|
|||
namespace Volo.Abp.Application.Services |
|||
{ |
|||
public interface IAsyncCrudAppService<TEntityDto, in TKey> |
|||
: IAsyncCrudAppService<TEntityDto, TKey, PagedAndSortedResultRequestDto> |
|||
where TEntityDto : IEntityDto<TKey> |
|||
{ |
|||
|
|||
} |
|||
|
|||
public interface IAsyncCrudAppService<TEntityDto, in TKey, in TGetListInput> |
|||
: IAsyncCrudAppService<TEntityDto, TKey, TGetListInput, TEntityDto, TEntityDto> |
|||
where TEntityDto : IEntityDto<TKey> |
|||
{ |
|||
|
|||
} |
|||
|
|||
public interface IAsyncCrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput> |
|||
: IAsyncCrudAppService<TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput> |
|||
where TEntityDto : IEntityDto<TKey> |
|||
{ |
|||
|
|||
} |
|||
|
|||
public interface IAsyncCrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput> |
|||
: IAsyncCrudAppService<TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput> |
|||
where TEntityDto : IEntityDto<TKey> |
|||
{ |
|||
|
|||
} |
|||
|
|||
public interface IAsyncCrudAppService<TGetOutputDto, TGetListOutputDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput> |
|||
: IApplicationService |
|||
where TGetOutputDto : IEntityDto<TKey> |
|||
where TGetListOutputDto : IEntityDto<TKey> |
|||
{ |
|||
Task<TGetOutputDto> GetAsync(TKey id); |
|||
|
|||
Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input); |
|||
|
|||
Task<TGetOutputDto> CreateAsync(TCreateInput input); |
|||
|
|||
Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input); |
|||
|
|||
Task DeleteAsync(TKey id); |
|||
} |
|||
} |
|||
@ -1,36 +0,0 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.EventBus |
|||
{ |
|||
public static class EventBusExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Triggers an event.
|
|||
/// </summary>
|
|||
/// <typeparam name="TEvent">Event type</typeparam>
|
|||
/// <param name="eventBus">Event bus instance</param>
|
|||
/// <param name="eventData">Related data for the event</param>
|
|||
public static void Publish<TEvent>([NotNull] this IEventBus eventBus, [NotNull] TEvent eventData) |
|||
where TEvent : class |
|||
{ |
|||
Check.NotNull(eventBus, nameof(eventBus)); |
|||
|
|||
AsyncHelper.RunSync(() => eventBus.PublishAsync(eventData)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Triggers an event.
|
|||
/// </summary>
|
|||
/// <param name="eventBus">Event bus instance</param>
|
|||
/// <param name="eventType">Event type</param>
|
|||
/// <param name="eventData">Related data for the event</param>
|
|||
public static void Publish([NotNull] this IEventBus eventBus, [NotNull] Type eventType, [NotNull] object eventData) |
|||
{ |
|||
Check.NotNull(eventBus, nameof(eventBus)); |
|||
|
|||
AsyncHelper.RunSync(() => eventBus.PublishAsync(eventType, eventData)); |
|||
} |
|||
} |
|||
} |
|||
@ -1,13 +0,0 @@ |
|||
using System.Collections.Generic; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.Localization |
|||
{ |
|||
public static class LanguageProviderExtensions |
|||
{ |
|||
public static IReadOnlyList<LanguageInfo> GetLanguages(this ILanguageProvider languageProvider) |
|||
{ |
|||
return AsyncHelper.RunSync(languageProvider.GetLanguagesAsync); |
|||
} |
|||
} |
|||
} |
|||
@ -1,21 +0,0 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.MultiTenancy |
|||
{ |
|||
public static class TenantStoreExtensions |
|||
{ |
|||
[CanBeNull] |
|||
public static TenantConfiguration Find(this ITenantStore tenantStore, string name) |
|||
{ |
|||
return AsyncHelper.RunSync(() => tenantStore.FindAsync(name)); |
|||
} |
|||
|
|||
[CanBeNull] |
|||
public static TenantConfiguration Find(this ITenantStore tenantStore, Guid id) |
|||
{ |
|||
return AsyncHelper.RunSync(() => tenantStore.FindAsync(id)); |
|||
} |
|||
} |
|||
} |
|||
@ -1,21 +0,0 @@ |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Threading |
|||
{ |
|||
public static class RunnableExtensions |
|||
{ |
|||
public static void Start([NotNull] this IRunnable runnable) |
|||
{ |
|||
Check.NotNull(runnable, nameof(runnable)); |
|||
|
|||
AsyncHelper.RunSync(() => runnable.StartAsync()); |
|||
} |
|||
|
|||
public static void Stop([NotNull] this IRunnable runnable) |
|||
{ |
|||
Check.NotNull(runnable, nameof(runnable)); |
|||
|
|||
AsyncHelper.RunSync(() => runnable.StopAsync()); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue