使用
| 用法 |
---|
ICrudAppService创建应用服务 | 例3 | IAsyncCrudAppService | 例1 | IApplicationService创建应用服务 | 例2 | 获取服务 | 例4 |
为Task实体创建一个CRUD应用服务
-
Task.cs public class Task : Entity, IHasCreationTime
{
public string Title { get; set; }
public string Description { get; set; }
public DateTime CreationTime { get; set; }
public TaskState State { get; set; }
public Person AssignedPerson { get; set; }
public Guid? AssignedPersonId { get; set; }
public Task()
{
CreationTime = Clock.Now;
State = TaskState.Open;
}
}
-
为该Task实体创建一个DTO [AutoMap(typeof(Task))]//用AutoMap特性,自动创建实体和DTO的映射配置
public class TaskDto : EntityDto, IHasCreationTime
{
public string Title { get; set; }
public string Description { get; set; }
public DateTime CreationTime { get; set; }
public TaskState State { get; set; }
public Guid? AssignedPersonId { get; set; }
public string AssignedPersonName { get; set; }
}
-
创建应用服务接口类 ITaskAppService.cs public interface ITaskAppService : IAsyncCrudAppService<TaskDto>
{ }
-
实现应用服务接口类 TaskAppService.cs public class TaskAppService : AsyncCrudAppService<Task, TaskDto>,ITaskAppService
{
public TaskAppService(IRepository<Task> repository)
: base(repository)//注入了仓储并且传递该参数给基类
{ }
}
- Application.Contracts\XXXs\IXXXAppService.cs
/// <summary>
/// 管理服务接口
/// </summary>
public interface IXXXAppService : IApplicationService
{
/// <summary>
/// 新增数据
/// </summary>
/// <param name="model">参数模型</param>
/// <returns>新增操作结果</returns>
Task<bool> InsertAsync(XXXDto model);
/// <summary>
/// 删除数据
/// </summary>
/// <param name="id">id</param>
/// <returns>删除操作结果</returns>
Task<bool> DeleteAsync(Guid id);
/// <summary>
/// 更新数据
/// </summary>
/// <param name="model">参数模型</param>
/// <returns>更新操作结果</returns>
Task<bool> UpdateAsync(XXXDto model);
/// <summary>
/// 根据id查询
/// </summary>
/// <param name="id">id</param>
/// <returns>查询结果</returns>
Task<XXXDto> GetAsync(Guid id);
/// <summary>
/// 获取分页
/// </summary>
/// <param name="dto">分页请求Dto</param>
/// <returns>分页结果</returns>
Task<PagedResultDto<XXXDto>> GetPagedListAsync(PagedResultRequestDto dto);
}
- Application\XXXs\XXXAppService.cs
public class XXXAppService : XXXXAppService, IXXXAppService
{
/// <summary>
/// 仓储
/// </summary>
private readonly IXXXRepository repository;
/// <summary>
/// Guid生成器
/// </summary>
private readonly IGuidGenerator guidGenerator;
/// <summary>
/// 初始化
/// </summary>
public XXXAppService(IXXXRepository repository,IGuidGenerator guidGenerator)
{
this.repository = repository;
this.guidGenerator = guidGenerator;
}
/// <summary>
/// 新增数据
/// </summary>
/// <param name="model">参数模型</param>
/// <returns>新增操作结果</returns>
public async Task<bool> InsertAsync(XXXDto model)
{
var newEntity = new XXX(GuidGenerator.Create(), model.Name, model.Author, model.Type);
EntityHelper.TrySetId((IEntity<Guid>)newEntity, () => guidGenerator.Create());
await repository.InsertAsync(newEntity);
return true;
}
/// <summary>
/// 新增多条数据
/// </summary>
/// <param name="modelList">参数模型列表</param>
/// <returns>新增操作结果</returns>
public async Task<bool> InsertManyAsync(List<XXXDto> modelList)
{
var newEntityList = ObjectMapper.Map<List<XXXDto>, List<XXX>>(modelList);
foreach (var entity in newEntityList)
{
EntityHelper.TrySetId(entity, () => guidGenerator.Create());
var find = await repository.FindAsync(p => p.SourceTable == entity.SourceTable && p.TargetTable == entity.TargetTable);
if (find != default)
{
continue;
}
await repository.InsertAsync(entity, true);
}
return true;
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="id">id</param>
/// <returns>删除操作结果</returns>
public async Task<bool> DeleteAsync(Guid id)
{
var find = await repository.FindAsync(id);
if (find == default)
{
throw new BusinessException("要删除的记录不存在!");
}
await repository.DeleteAsync(find, true);
return true;
}
/// <summary>
/// 删除所有数据
/// </summary>
/// <returns>删除操作结果</returns>
public async Task<bool> DeleteManyAsync()
{
var list = (await repository.GetListAsync()).Select(x => x.Id).ToList();
await repository.DeleteManyAsync(list,true);
return true;
}
/// <summary>
/// 更新数据
/// </summary>
/// <param name="model">参数模型</param>
/// <returns>更新操作结果</returns>
public async Task<bool> UpdateAsync(XXXDto model)
{
var find = await repository.FindAsync(model.Id);
if (find == default)
{
throw new BusinessException("要修改的记录不存在");
}
find.Update(
model.Config,
model.RegisterType,
model.RegisterSubtype,
model.SingleCertificate,
model.Batch,
model.Media,model.CustomizeQuery,
ObjectMapper.Map<List<ParameterConfigurationDto>, List<ParameterConfiguration>>(model.ParameterConfig)
);
await repository.UpdateAsync(find);
return true;
}
/// <summary>
/// 根据id查询
/// </summary>
/// <param name="id">id</param>
/// <returns>查询结果</returns>
public async Task<XXXDto> GetAsync(Guid id)
{
var find = await repository.FindAsync(id);
if (find == default)
{
throw new BusinessException("所查询的记录不存在");
}
return ObjectMapper.Map<XXX,XXXDto>(find);
}
/// <summary>
/// 获取分页
/// </summary>
/// <param name="dto">分页请求Dto</param>
/// <returns>分页结果</returns>
public PagedResultDto<XXXDto> GetPagedList(PagedResultRequestDto dto)
{
var count = repository.Count();
var t = repository
.Where(
x=> !x.IsDeleted)
.OrderByDescending(x => x.RegisterType)
.PageBy(dto)
.ToList();
return new PagedResultDto<XXXDto> {
TotalCount = count,
Items = ObjectMapper.Map<List<XXX>, List<XXXDto>>(t)
};
}
/// <summary>
/// 异步获取分页
/// </summary>
/// <param name="dto">分页请求Dto</param>
/// <returns>分页结果</returns>
public async Task<PagedResultDto<EnterpriseInfoDto>> GetPagedList(PagedResultRequestDto dto)
{
var count = await repository.GetCountAsync();
var t = await repository.GetPagedListAsync(dto.SkipCount,dto.MaxResultCount,nameof(RegisterTypeConfig.TemplateId));
return new PagedResultDto<EnterpriseInfoDto>
{
TotalCount = count,
Items = ObjectMapper.Map<List<EnterpriseInfo>, List<EnterpriseInfoDto>>(t)
};
}
}
- Application\Books\BookAppService.cs
public interface IBookAppService :
ICrudAppService< //Defines CRUD methods
BookDto, //Used to show books
Guid, //Primary key of the book entity
PagedAndSortedResultRequestDto, //Used for paging/sorting
CreateUpdateBookDto> //Used to create/update a book
{
}
- Application.Contracts\Books\IBookAppService.cs
public class BookAppService :
CrudAppService< //CrudAppService实现 `ICrudAppService` 定义的CRUD方法
Book, //The Book entity
BookDto, //Used to show books
Guid, //Primary key of the book entity
PagedAndSortedResultRequestDto, //Used for paging/sorting
CreateUpdateBookDto>, //Used to create/update a book
IBookAppService //implement the IBookAppService
{
public BookAppService(IRepository<Book, Guid> repository)
: base(repository)
{
}
}
利用IServiceProvider获取服务
public class UserService<T> : IUserService<T> where T : IUserMessage
{
private readonly IServiceProvider _serviceProvider;
public UserService(ServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public async Task UpdateUser(int id)
{
var userRoleService = _serviceProvider.GetRequiredService<IUserRoleService>();
var roles = await userRoleService.GetRoleForUser(id);
}
}
|