当前位置:   article > 正文

怎样优雅地增删查改(三):业务用户的增数据结构就下面几个删查改

怎样优雅地增删查改(三):业务用户的增数据结构就下面几个删查改

创建业务用户

区别于身份管理模块(Identity模块)的鉴权用户IdentityUser,业务用户(BusinessUser)是围绕业务系统中“用户”这一定义的领域模型。如:在一个医院系统中,业务用户可以是医生、护士、患者;在一个OA系统中,业务用户可以是员工、管理员、客户等。

业务用户和鉴权用户由同步机制关联,业务用户通过分布式事件(DistributedEvent)的同步器(Synchronizer)与鉴权用户关联同步。

在Health业务模块中,定义两种业务用户:

Client: 客户;

Employee: 员工。

这些业务用户继承自HealthUser,HealthUser是业务用户的基类,包含了业务用户的基本信息,如姓名,性别,出生日期,身份证号等。并且需要实现IUpdateUserData接口,以便在同步鉴权用户信息时,更新业务用户的基本信息。

Employee包含工号,职称,简介等信息。其领域模型定义如下:

  1. public class Employee : HealthUser<Guid>, IUser, IUpdateUserData
  2. {
  3. [StringLength(12)]
  4. public string EmployeeNumber { get; set; }
  5. [StringLength(64)]
  6. public string EmployeeTitle { get; set; }
  7. public string Introduction { get; set; }
  8. ...
  9. }

Client包含客户号,身高,体重,婚姻状况等信息。其领域模型定义如下:

  1. public class Client : HealthUser<Guid>, IUser, IUpdateUserData
  2. {
  3. //unique
  4. [StringLength(12)]
  5. public string ClientNumber { get; set; }
  6. public string ClientNumberType { get; set; }
  7. [Range(0.0, 250.0)]
  8. public double? Height { get; set; }
  9. [Range(0.0, 1000.0)]
  10. public double? Weight { get; set; }
  11. public string Marriage { get; set; }
  12. public string Status { get; set; }
  13. }

创建业务用户同步器

以Client为例,ClientLookupService是业务用户的查询服务,其基类UserLookupService定义了关联用户的查询接口,包括按ID查询,按用户名查询,按组织架构查询,按户关系查询等。

创建ClientLookupService, 代码如下

  1. public class ClientLookupService : UserLookupService<Client, IClientRepository>, IClientLookupService
  2. {
  3. public ClientLookupService(
  4. IClientRepository userRepository,
  5. IUnitOfWorkManager unitOfWorkManager)
  6. : base(
  7. userRepository,
  8. unitOfWorkManager)
  9. {
  10. }
  11. protected override Client CreateUser(IUserData externalUser)
  12. {
  13. return new Client(externalUser);
  14. }
  15. }

同步器订阅了分布式事件EntityUpdatedEto,当鉴权用户更新时,同步器将更新业务用户的基本信息。

创建ClientSynchronizer,代码如下

  1. public class ClientSynchronizer :
  2. IDistributedEventHandler<EntityUpdatedEto<UserEto>>,
  3. ITransientDependency
  4. {
  5. protected IClientRepository UserRepository { get; }
  6. protected IClientLookupService UserLookupService { get; }
  7. public ClientSynchronizer(
  8. IClientRepository userRepository,
  9. IClientLookupService userLookupService)
  10. {
  11. UserRepository = userRepository;
  12. UserLookupService = userLookupService;
  13. }
  14. public async Task HandleEventAsync(EntityUpdatedEto<UserEto> eventData)
  15. {
  16. var user = await UserRepository.FindAsync(eventData.Entity.Id);
  17. if (user != null)
  18. {
  19. if (user.Update(eventData.Entity))
  20. {
  21. await UserRepository.UpdateAsync(user);
  22. }
  23. }
  24. }
  25. }

创建业务用户应用服务

以Employee为例

在应用层中创建EmployeeAppService,在这里我们实现对业务用户的增删改查操作。

EmployeeAppService继承自CrudAppService,它是ABP框架提供的增删改查的基类,其基类定义了增删改查的接口,包括GetAsync,GetListAsync,CreateAsync,UpdateAsync,DeleteAsync等。

OrganizationUnit为业务用户的查询接口的按组织架构查询提供查询依据。OrganizationUnitAppService注入到EmployeeAppService中。

  1. public class EmployeeAppService : CrudAppService<Employee, EmployeeDto, Guid, GetAllEmployeeInput, CreateEmployeeInput>, IEmployeeAppService
  2. {
  3. private readonly IOrganizationUnitAppService organizationUnitAppService;
  4. }

创建CreateWithUserAsync方法,用于创建业务用户。

  1. public async Task<EmployeeDto> CreateWithUserAsync(CreateEmployeeWithUserInput input)
  2. {
  3. var createdUser = await identityUserAppService.CreateAsync(input);
  4. await CurrentUnitOfWork.SaveChangesAsync();
  5. var currentEmployee = await userLookupService.FindByIdAsync(createdUser.Id);
  6. ObjectMapper.Map(input, currentEmployee);
  7. var updatedEmployee = await Repository.UpdateAsync(currentEmployee);
  8. var result = ObjectMapper.Map<Employee, EmployeeDto>(updatedEmployee);
  9. if (input.OrganizationUnitId.HasValue)
  10. {
  11. await organizationUnitAppService.AddToOrganizationUnitAsync(
  12. new UserToOrganizationUnitInput()
  13. { UserId = createdUser.Id, OrganizationUnitId = input.OrganizationUnitId.Value });
  14. }
  15. return result;
  16. }

删除接口由CrudAppService提供默认实现,无需重写。

创建UpdateWithUserAsync方法,用于更新业务用户。

  1. public async Task<EmployeeDto> UpdateWithUserAsync(CreateEmployeeInput input)
  2. {
  3. var currentEmployee = await userLookupService.FindByIdAsync(input.Id);
  4. if (currentEmployee == null)
  5. {
  6. throw new UserFriendlyException("没有找到对应的用户");
  7. }
  8. ObjectMapper.Map(input, currentEmployee);
  9. var updatedEmployee = await Repository.UpdateAsync(currentEmployee);
  10. var result = ObjectMapper.Map<Employee, EmployeeDto>(updatedEmployee);
  11. return result;
  12. }

查询单个实体接口由CrudAppService提供默认实现,无需重写。

查询集合:

以Employee为例,查询接口所需要的入参为:

OrganizationUnitId:按组织架构查询用户
IsWithoutOrganization:查询不属于任何组织架构的用户
EmployeeTitle:按职称查询用户

创建GetAllEmployeeInput,代码如下

  1. public class GetAllEmployeeInput : PagedAndSortedResultRequestDto
  2. {
  3. public string EmployeeTitle { get; set; }
  4. public Guid? OrganizationUnitId { get; set; }
  5. public bool IsWithoutOrganization { get; set; }
  6. }

重写CreateFilteredQueryAsync

  1. protected override async Task<IQueryable<Employee>> CreateFilteredQueryAsync(GetAllEmployeeInput input)
  2. {
  3. var query = await ReadOnlyRepository.GetQueryableAsync().ConfigureAwait(continueOnCapturedContext: false);
  4. if (input.OrganizationUnitId.HasValue && !input.IsWithoutOrganization)
  5. {
  6. var organizationUnitUsers = await organizationUnitAppService.GetOrganizationUnitUsersAsync(new GetOrganizationUnitUsersInput()
  7. {
  8. Id = input.OrganizationUnitId.Value
  9. });
  10. if (organizationUnitUsers.Count() > 0)
  11. {
  12. var ids = organizationUnitUsers.Select(c => c.Id);
  13. query = query.Where(t => ids.Contains(t.Id));
  14. }
  15. else
  16. {
  17. query = query.Where(c => false);
  18. }
  19. }
  20. else if (input.IsWithoutOrganization)
  21. {
  22. var organizationUnitUsers = await organizationUnitAppService.GetUsersWithoutOrganizationAsync(new GetUserWithoutOrganizationInput());
  23. if (organizationUnitUsers.Count() > 0)
  24. {
  25. var ids = organizationUnitUsers.Select(c => c.Id);
  26. query = query.Where(t => ids.Contains(t.Id));
  27. }
  28. else
  29. {
  30. query = query.Where(c => false);
  31. }
  32. }
  33. query = query.WhereIf(!string.IsNullOrEmpty(input.EmployeeTitle), c => c.EmployeeTitle == input.EmployeeTitle);
  34. return query;
  35. }

至此,我们已完成了对业务用户的增删改查功能实现。

创建控制器

在HttpApi项目中创建EmployeeController,代码如下:

  1. [Area(HealthRemoteServiceConsts.ModuleName)]
  2. [RemoteService(Name = HealthRemoteServiceConsts.RemoteServiceName)]
  3. [Route("api/Health/employee")]
  4. public class EmployeeController : AbpControllerBase, IEmployeeAppService
  5. {
  6. private readonly IEmployeeAppService _employeeAppService;
  7. public EmployeeController(IEmployeeAppService employeeAppService)
  8. {
  9. _employeeAppService = employeeAppService;
  10. }
  11. [HttpPost]
  12. [Route("CreateWithUser")]
  13. public Task<EmployeeDto> CreateWithUserAsync(CreateEmployeeWithUserInput input)
  14. {
  15. return _employeeAppService.CreateWithUserAsync(input);
  16. }
  17. [HttpDelete]
  18. [Route("Delete")]
  19. public Task DeleteAsync(Guid id)
  20. {
  21. return _employeeAppService.DeleteAsync(id);
  22. }
  23. [HttpPut]
  24. [Route("UpdateWithUser")]
  25. public Task<EmployeeDto> UpdateWithUserAsync(CreateEmployeeInput input)
  26. {
  27. return _employeeAppService.UpdateWithUserAsync(input);
  28. }
  29. [HttpGet]
  30. [Route("Get")]
  31. public Task<EmployeeDto> GetAsync(Guid id)
  32. {
  33. return _employeeAppService.GetAsync(id);
  34. }
  35. [HttpGet]
  36. [Route("GetAll")]
  37. public Task<PagedResultDto<EmployeeDto>> GetAllAsync(GetAllEmployeeInput input)
  38. {
  39. return _employeeAppService.GetAllAsync(input);
  40. }
  41. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/573225
推荐阅读
相关标签
  

闽ICP备14008679号