chore:目录重构

This commit is contained in:
陈淳
2023-04-15 17:35:22 +08:00
parent a612af4f68
commit fb27fb8aa4
238 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IEntityDto
{
}
public interface IEntityDto<TKey> : IEntityDto
{
TKey Id { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IHasTotalCount
{
long Total { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IListResult<T>
{
IReadOnlyList<T> Items { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Services.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IPagedAllResultRequestDto : IPageTimeResultRequestDto, IPagedAndSortedResultRequestDto
{
}
}

View File

@@ -0,0 +1,15 @@

using Yi.Framework.Infrastructure.Enums;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IPagedAndSortedResultRequestDto
{
int PageNum { get; set; }
int PageSize { get; set; }
string? SortBy { get; set; }
OrderByEnum SortType { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IPagedResult<T> : IListResult<T>, IHasTotalCount
{
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
[Serializable]
public abstract class EntityDto<TKey> : EntityDto, IEntityDto<TKey>, IEntityDto
{
//
// 摘要:
// Id of the entity.
public TKey Id { get; set; }
public override string ToString()
{
return $"[DTO: {GetType().Name}] Id = {Id}";
}
}
[Serializable]
public abstract class EntityDto : IEntityDto
{
public override string ToString()
{
return "[DTO: " + GetType().Name + "]";
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
[Serializable]
public class ListResultDto<T> : IListResult<T>
{
public IReadOnlyList<T> Items
{
get { return _items ?? (_items = new List<T>()); }
set { _items = value; }
}
private IReadOnlyList<T> _items;
public ListResultDto()
{
}
public ListResultDto(IReadOnlyList<T> items)
{
Items = items;
}
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Ddd.Services.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
public class PagedAllResultRequestDto : PagedAndSortedResultRequestDto, IPagedAllResultRequestDto, IPagedAndSortedResultRequestDto, IPageTimeResultRequestDto
{
/// <summary>
/// 查询开始时间条件
/// </summary>
public DateTime? StartTime { get; set; }
/// <summary>
/// 查询结束时间条件
/// </summary>
public DateTime? EndTime { get; set; }
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Enums;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
public class PagedAndSortedResultRequestDto : IPagedAndSortedResultRequestDto
{
/// <summary>
/// 查询当前页条件
/// </summary>
public int PageNum { get; set; } = 1;
/// <summary>
/// 查询分页大小条件
/// </summary>
public int PageSize { get; set; } = int.MaxValue;
/// <summary>
/// 查询排序字段条件
/// </summary>
public string? SortBy { get; set; }
/// <summary>
/// 查询排序类别条件
/// </summary>
public OrderByEnum SortType { get; set; } = OrderByEnum.Desc;
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
public class PagedDto<T>
{
public PagedDto(long totalCount, List<T> items)
{
Total = totalCount;
Items = items;
}
public long Total { get; set; }
public List<T> Items { get; set; }
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
public class PagedResultDto<T> : ListResultDto<T>, IPagedResult<T>
{
public long Total { get; set; }
public PagedResultDto()
{
}
public PagedResultDto(long totalCount, IReadOnlyList<T> items)
: base(items)
{
Total = totalCount;
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Entities
{
public class AggregateRoot : IEntity, IAggregateRoot
{
}
public class AggregateRoot<TKey> : Entity<TKey>, IEntity<TKey>
{
}
}

View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Entities
{
[Serializable]
public abstract class Entity : IEntity
{
protected Entity()
{
}
public override string ToString()
{
return "[ENTITY: " + GetType().Name + "] Keys = " + GetKeys();
}
public abstract object[] GetKeys();
//实体比较简化
//public bool EntityEquals(IEntity other)
//{
// return this.GetKeys().Equals(other.GetKeys());
//}
}
[Serializable]
public abstract class Entity<TKey> : Entity, IEntity<TKey>, IEntity
{
public virtual TKey Id { get; set; }
protected Entity()
{
}
protected Entity(TKey id)
{
Id = id;
}
public override object[] GetKeys()
{
return new object[1] { Id };
}
public override string ToString()
{
return $"[ENTITY: {GetType().Name}] Id = {Id}";
}
}
}

View File

@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Entities
{
public interface IAggregateRoot : IEntity
{
}
public interface IAggregateRoot<TKey> : IEntity<TKey>
{
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Entities
{
public interface IEntity
{
//
// 摘要:
// Returns an array of ordered keys for this entity.
}
public interface IEntity<TKey> : IEntity
{
//
// 摘要:
// Unique identifier for this entity.
TKey Id { get; set; }
}
}

View File

@@ -0,0 +1,57 @@
using System.Linq.Expressions;
using SqlSugar;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Enums;
namespace Yi.Framework.Infrastructure.Ddd.Repositories
{
public interface IRepository<T>
{
/// <summary>
/// 注释一下严格意义这里应该protected但是我认为 简易程度 与 耦合程度 中是需要进行衡量的
/// </summary>
ISugarQueryable<T> _DbQueryable { get; }
//单查
Task<T> GetByIdAsync(dynamic id);
Task<T> GetSingleAsync(Expression<Func<T, bool>> whereExpression);
Task<T> GetFirstAsync(Expression<Func<T, bool>> whereExpression);
Task<bool> IsAnyAsync(Expression<Func<T, bool>> whereExpression);
Task<int> CountAsync(Expression<Func<T, bool>> whereExpression);
//多查
Task<List<T>> GetListAsync();
Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression);
//分页查
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize, Expression<Func<T, object>>? orderByExpression = null, OrderByEnum orderByType = OrderByEnum.Asc);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, Expression<Func<T, object>>? orderByExpression = null, OrderByEnum orderByType = OrderByEnum.Asc);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc);
//插入
Task<bool> InsertAsync(T insertObj);
Task<bool> InsertOrUpdateAsync(T data);
Task<bool> InsertOrUpdateAsync(List<T> datas);
Task<int> InsertReturnIdentityAsync(T insertObj);
Task<long> InsertReturnBigIdentityAsync(T insertObj);
Task<long> InsertReturnSnowflakeIdAsync(T insertObj);
Task<T> InsertReturnEntityAsync(T insertObj);
Task<bool> InsertRangeAsync(List<T> insertObjs);
//更新
Task<bool> UpdateAsync(T updateObj);
Task<bool> UpdateRangeAsync(List<T> updateObjs);
Task<bool> UpdateAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression);
Task<bool> UpdateIgnoreNullAsync(T updateObj);
//删除
Task<bool> DeleteAsync(T deleteObj);
Task<bool> DeleteAsync(List<T> deleteObjs);
Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression);
Task<bool> DeleteByIdAsync(dynamic id);
Task<bool> DeleteByIdsAsync(dynamic[] ids);
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface IApplicationService
{
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface ICreateAppService<TEntityDto>
: ICreateAppService<TEntityDto, TEntityDto>
{
}
public interface ICreateAppService<TGetOutputDto, in TCreateInput>
: IApplicationService
{
Task<TGetOutputDto> CreateAsync(TCreateInput input);
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface ICreateUpdateAppService<TEntityDto, in TKey>
: ICreateUpdateAppService<TEntityDto, TKey, TEntityDto, TEntityDto>
{
}
public interface ICreateUpdateAppService<TEntityDto, in TKey, in TCreateUpdateInput>
: ICreateUpdateAppService<TEntityDto, TKey, TCreateUpdateInput, TCreateUpdateInput>
{
}
public interface ICreateUpdateAppService<TGetOutputDto, in TKey, in TCreateUpdateInput, in TUpdateInput>
: ICreateAppService<TGetOutputDto, TCreateUpdateInput>,
IUpdateAppService<TGetOutputDto, TKey, TUpdateInput>
{
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface ICrudAppService<TEntityDto, in TKey>
: ICrudAppService<TEntityDto, TKey, PagedAndSortedResultRequestDto>
{
}
public interface ICrudAppService<TEntityDto, in TKey, in TGetListInput>
: ICrudAppService<TEntityDto, TKey, TGetListInput, TEntityDto>
{
}
public interface ICrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput>
: ICrudAppService<TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
{
}
public interface ICrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
: ICrudAppService<TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
{
}
public interface ICrudAppService<TGetOutputDto, TGetListOutputDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
: IReadOnlyAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput>,
ICreateUpdateAppService<TGetOutputDto, TKey, TCreateInput, TUpdateInput>,
IDeleteAppService<TKey>
{
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface IDeleteAppService<in TKey> : IApplicationService
{
Task<bool> DeleteAsync(string id);
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface IPageTimeResultRequestDto : IPagedAndSortedResultRequestDto
{
DateTime? StartTime { get; set; }
DateTime? EndTime { get; set; }
}
}

View File

@@ -0,0 +1,24 @@
using Yi.Framework.Infrastructure.Ddd.Dtos;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface IReadOnlyAppService<TEntityDto, in TKey>
: IReadOnlyAppService<TEntityDto, TEntityDto, TKey, PagedAndSortedResultRequestDto>
{
}
public interface IReadOnlyAppService<TEntityDto, in TKey, in TGetListInput>
: IReadOnlyAppService<TEntityDto, TEntityDto, TKey, TGetListInput>
{
}
public interface IReadOnlyAppService<TGetOutputDto, TGetListOutputDto, in TKey, in TGetListInput>
: IApplicationService
{
Task<TGetOutputDto> GetAsync(TKey id);
Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input);
}
}

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface IUpdateAppService<TEntityDto, in TKey>
: IUpdateAppService<TEntityDto, TKey, TEntityDto>
{
}
public interface IUpdateAppService<TGetOutputDto, in TKey, in TUpdateInput>
: IApplicationService
{
Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input);
}
}

View File

@@ -0,0 +1,11 @@
using Furion;
using MapsterMapper;
using Microsoft.Extensions.DependencyInjection;
namespace Yi.Framework.Infrastructure.Ddd.Services
{
public abstract class ApplicationService
{
public IMapper _mapper { get => App.GetRequiredService<IMapper>(); }
}
}

View File

@@ -0,0 +1,176 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Ddd.Entities;
using Yi.Framework.Infrastructure.Ddd.Services.Abstract;
using Yi.Framework.Infrastructure.Helper;
namespace Yi.Framework.Infrastructure.Ddd.Services
{
public abstract class CrudAppService<TEntity, TEntityDto, TKey>
: CrudAppService<TEntity, TEntityDto, TKey, PagedAndSortedResultRequestDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
}
public abstract class CrudAppService<TEntity, TEntityDto, TKey, TGetListInput>
: CrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TEntityDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
}
public abstract class CrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput>
: CrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
}
public abstract class CrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected override Task<TEntityDto> MapToGetListOutputDtoAsync(TEntity entity)
{
return MapToGetOutputDtoAsync(entity);
}
}
public abstract class CrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: ReadOnlyAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput>,
ICrudAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
protected virtual Task<TEntity> MapToEntityAsync(TGetListInput getListinput)
{
return Task.FromResult(_mapper.Map<TEntity>(getListinput));
}
protected virtual Task<TEntity> MapToEntityAsync(TCreateInput createInput)
{
var entity = _mapper.Map<TEntity>(createInput);
//这里判断实体的T给id赋值
//雪花id
if (entity is IEntity<long> entityForlongId)
{
if (entityForlongId.Id is default(long))
{
//使用反射暂时先使用sqlsuga的雪花id提供
//ps: linshi
ReflexHelper.SetModelValue(nameof(IEntity<long>.Id), SnowflakeHelper.NextId, entity);
}
}
if (entity is IEntity<Guid> entityForGuidId)
{
if (entityForGuidId.Id == Guid.Empty)
{
ReflexHelper.SetModelValue(nameof(IEntity<long>.Id), new Guid(), entity);
}
}
return Task.FromResult(entity);
}
protected virtual Task MapToEntityAsync(TUpdateInput updateInput, TEntity entity)
{
_mapper.Map(updateInput, entity);
return Task.CompletedTask;
}
protected virtual Task<TEntity> MapToEntityAsync(TUpdateInput updateInput)
{
var entity = _mapper.Map<TEntity>(updateInput);
return Task.FromResult(entity);
}
/// <summary>
/// 增
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<TGetOutputDto> CreateAsync(TCreateInput input)
{
var entity = await MapToEntityAsync(input);
//这里还可以设置租户
await _repository.InsertAsync(entity);
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 单、多删
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public virtual async Task<bool> DeleteAsync(string id)
{
if (id is null)
{
throw new ArgumentNullException(nameof(id));
}
var idsValue = id.Split(',');
if (idsValue is null || idsValue.Length == 0)
{
throw new ArgumentNullException(nameof(id));
}
return await _repository.DeleteByIdsAsync(idsValue.Select(x => (object)x!).ToArray());
}
///// <summary>
///// 删
///// </summary>
///// <param name="id"></param>
///// <returns></returns>
///// <exception cref="ArgumentNullException"></exception>
//public async Task<bool> DeleteAsync(TKey id)
//{
// if (id is null)
// {
// throw new ArgumentNullException(nameof(id));
// }
// return await _repository.DeleteByIdAsync(id);
//}
/// <summary>
/// 改
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public virtual async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input)
{
if (id is null)
{
throw new ArgumentNullException(nameof(id));
}
var entity = await _repository.GetByIdAsync(id);
await MapToEntityAsync(input, entity);
await _repository.UpdateAsync(entity);
return await MapToGetOutputDtoAsync(entity);
}
}
}

View File

@@ -0,0 +1,129 @@
using Furion;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using Yi.Framework.Infrastructure.Ddd.Dtos;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Ddd.Entities;
using Yi.Framework.Infrastructure.Ddd.Repositories;
using Yi.Framework.Infrastructure.Ddd.Services.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Services
{
public abstract class ReadOnlyAppService<TEntity, TEntityDto, TKey>
: ReadOnlyAppService<TEntity, TEntityDto, TEntityDto, TKey, PagedAndSortedResultRequestDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
}
public abstract class ReadOnlyAppService<TEntity, TEntityDto, TKey, TGetListInput>
: ReadOnlyAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
}
public abstract class ReadOnlyAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput> : ApplicationService,
IReadOnlyAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput>
where TEntity : class, IEntity
{
/// <summary>
/// 先暂时用服务定位的方式,之后将更改为属性注入
/// </summary>
protected IRepository<TEntity> _repository { get => App.GetRequiredService<IRepository<TEntity>>(); }
protected ISugarQueryable<TEntity> _DbQueryable => _repository._DbQueryable;
//Mapper
protected virtual Task<TGetOutputDto> MapToGetOutputDtoAsync(TEntity entity)
{
return Task.FromResult(_mapper.Map<TEntity, TGetOutputDto>(entity));
}
protected virtual Task<List<TGetListOutputDto>> MapToGetListOutputDtosAsync(List<TEntity> entities)
{
var dtos = _mapper.Map<List<TGetListOutputDto>>(entities);
return Task.FromResult(dtos);
}
protected virtual Task<TGetListOutputDto> MapToGetListOutputDtoAsync(TEntity entity)
{
var dto = _mapper.Map<TEntity, TGetListOutputDto>(entity);
return Task.FromResult(dto);
}
/// <summary>
/// 单查
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public virtual async Task<TGetOutputDto> GetAsync(TKey id)
{
if (id is null)
{
throw new ArgumentNullException(nameof(id));
}
var entity = await _repository.GetByIdAsync(id);
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 多查
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<PagedResultDto<TGetListOutputDto>> GetListAsync([FromQuery]TGetListInput input)
{
var totalCount = -1;
var entities = new List<TEntity>();
var entityDtos = new List<TGetListOutputDto>();
bool isPageList = true;
//if (totalCount > 0)
//{
//这里还可以追加如果是审计日志,继续拼接条件即可
if (input is IPageTimeResultRequestDto timeInput)
{
if (timeInput.StartTime is not null)
{
timeInput.EndTime = timeInput.EndTime ?? DateTime.Now;
}
}
if (input is IPagedAndSortedResultRequestDto sortInput)
{
entities = await _repository.GetPageListAsync(_ => true, sortInput, sortInput.SortBy, sortInput.SortType);
}
else
{
isPageList = false;
entities = await _repository.GetListAsync();
}
entityDtos = await MapToGetListOutputDtosAsync(entities);
//}
//如果是分页查询,还需要统计数量
if (isPageList)
{
totalCount = await _repository.CountAsync(_ => true);
}
return new PagedResultDto<TGetListOutputDto>(
totalCount,
entityDtos
);
}
}
}