@
區別於身份管理模組(Identity模組)的鑑權使用者IdentityUser,業務使用者(BusinessUser)是圍繞業務系統中「使用者」這一定義的領域模型。如:在一個醫院系統中,業務使用者可以是醫生、護士、患者;在一個OA系統中,業務使用者可以是員工、管理員、客戶等。
業務使用者和鑑權使用者由同步機制關聯,業務使用者通過分散式事件(DistributedEvent)的同步器(Synchronizer)與鑑權使用者關聯同步。
在Health業務模組中,定義兩種業務使用者:
Client: 客戶;
Employee: 員工。
這些業務使用者繼承自HealthUser,HealthUser是業務使用者的基礎類別,包含了業務使用者的基本資訊,如姓名,性別,出生日期,身份證號等。並且需要實現IUpdateUserData介面,以便在同步鑑權使用者資訊時,更新業務使用者的基本資訊。
Employee包含工號,職稱,簡介等資訊。其領域模型定義如下:
public class Employee : HealthUser<Guid>, IUser, IUpdateUserData
{
[StringLength(12)]
public string EmployeeNumber { get; set; }
[StringLength(64)]
public string EmployeeTitle { get; set; }
public string Introduction { get; set; }
...
}
Client包含客戶號,身高,體重,婚姻狀況等資訊。其領域模型定義如下:
public class Client : HealthUser<Guid>, IUser, IUpdateUserData
{
//unique
[StringLength(12)]
public string ClientNumber { get; set; }
public string ClientNumberType { get; set; }
[Range(0.0, 250.0)]
public double? Height { get; set; }
[Range(0.0, 1000.0)]
public double? Weight { get; set; }
public string Marriage { get; set; }
public string Status { get; set; }
}
以Client為例,ClientLookupService是業務使用者的查詢服務,其基礎類別UserLookupService定義了關聯使用者的查詢介面,包括按ID查詢,按使用者名稱查詢,按組織架構查詢,按戶關係查詢等。
建立ClientLookupService, 程式碼如下
public class ClientLookupService : UserLookupService<Client, IClientRepository>, IClientLookupService
{
public ClientLookupService(
IClientRepository userRepository,
IUnitOfWorkManager unitOfWorkManager)
: base(
userRepository,
unitOfWorkManager)
{
}
protected override Client CreateUser(IUserData externalUser)
{
return new Client(externalUser);
}
}
同步器訂閱了分散式事件EntityUpdatedEto
建立ClientSynchronizer,程式碼如下
public class ClientSynchronizer :
IDistributedEventHandler<EntityUpdatedEto<UserEto>>,
ITransientDependency
{
protected IClientRepository UserRepository { get; }
protected IClientLookupService UserLookupService { get; }
public ClientSynchronizer(
IClientRepository userRepository,
IClientLookupService userLookupService)
{
UserRepository = userRepository;
UserLookupService = userLookupService;
}
public async Task HandleEventAsync(EntityUpdatedEto<UserEto> eventData)
{
var user = await UserRepository.FindAsync(eventData.Entity.Id);
if (user != null)
{
if (user.Update(eventData.Entity))
{
await UserRepository.UpdateAsync(user);
}
}
}
}
以Employee為例
在應用層中建立EmployeeAppService,在這裡我們實現對業務使用者的增刪改查操作。
EmployeeAppService繼承自CrudAppService,它是ABP框架提供的增刪改查的基礎類別,其基礎類別定義了增刪改查的介面,包括GetAsync,GetListAsync,CreateAsync,UpdateAsync,DeleteAsync等。
OrganizationUnit為業務使用者的查詢介面的按組織架構查詢提供查詢依據。OrganizationUnitAppService注入到EmployeeAppService中。
public class EmployeeAppService : CrudAppService<Employee, EmployeeDto, Guid, GetAllEmployeeInput, CreateEmployeeInput>, IEmployeeAppService
{
private readonly IOrganizationUnitAppService organizationUnitAppService;
}
建立CreateWithUserAsync方法,用於建立業務使用者。
public async Task<EmployeeDto> CreateWithUserAsync(CreateEmployeeWithUserInput input)
{
var createdUser = await identityUserAppService.CreateAsync(input);
await CurrentUnitOfWork.SaveChangesAsync();
var currentEmployee = await userLookupService.FindByIdAsync(createdUser.Id);
ObjectMapper.Map(input, currentEmployee);
var updatedEmployee = await Repository.UpdateAsync(currentEmployee);
var result = ObjectMapper.Map<Employee, EmployeeDto>(updatedEmployee);
if (input.OrganizationUnitId.HasValue)
{
await organizationUnitAppService.AddToOrganizationUnitAsync(
new UserToOrganizationUnitInput()
{ UserId = createdUser.Id, OrganizationUnitId = input.OrganizationUnitId.Value });
}
return result;
}
刪除介面由CrudAppService提供預設實現,無需重寫。
建立UpdateWithUserAsync方法,用於更新業務使用者。
public async Task<EmployeeDto> UpdateWithUserAsync(CreateEmployeeInput input)
{
var currentEmployee = await userLookupService.FindByIdAsync(input.Id);
if (currentEmployee == null)
{
throw new UserFriendlyException("沒有找到對應的使用者");
}
ObjectMapper.Map(input, currentEmployee);
var updatedEmployee = await Repository.UpdateAsync(currentEmployee);
var result = ObjectMapper.Map<Employee, EmployeeDto>(updatedEmployee);
return result;
}
查詢單個實體介面由CrudAppService提供預設實現,無需重寫。
查詢集合:
以Employee為例,查詢介面所需要的入參為:
OrganizationUnitId:按組織架構查詢使用者
IsWithoutOrganization:查詢不屬於任何組織架構的使用者
EmployeeTitle:按職稱查詢使用者
建立GetAllEmployeeInput,程式碼如下
public class GetAllEmployeeInput : PagedAndSortedResultRequestDto
{
public string EmployeeTitle { get; set; }
public Guid? OrganizationUnitId { get; set; }
public bool IsWithoutOrganization { get; set; }
}
重寫CreateFilteredQueryAsync
protected override async Task<IQueryable<Employee>> CreateFilteredQueryAsync(GetAllEmployeeInput input)
{
var query = await ReadOnlyRepository.GetQueryableAsync().ConfigureAwait(continueOnCapturedContext: false);
if (input.OrganizationUnitId.HasValue && !input.IsWithoutOrganization)
{
var organizationUnitUsers = await organizationUnitAppService.GetOrganizationUnitUsersAsync(new GetOrganizationUnitUsersInput()
{
Id = input.OrganizationUnitId.Value
});
if (organizationUnitUsers.Count() > 0)
{
var ids = organizationUnitUsers.Select(c => c.Id);
query = query.Where(t => ids.Contains(t.Id));
}
else
{
query = query.Where(c => false);
}
}
else if (input.IsWithoutOrganization)
{
var organizationUnitUsers = await organizationUnitAppService.GetUsersWithoutOrganizationAsync(new GetUserWithoutOrganizationInput());
if (organizationUnitUsers.Count() > 0)
{
var ids = organizationUnitUsers.Select(c => c.Id);
query = query.Where(t => ids.Contains(t.Id));
}
else
{
query = query.Where(c => false);
}
}
query = query.WhereIf(!string.IsNullOrEmpty(input.EmployeeTitle), c => c.EmployeeTitle == input.EmployeeTitle);
return query;
}
至此,我們已完成了對業務使用者的增刪改查功能實現。
在HttpApi專案中建立EmployeeController,程式碼如下:
[Area(HealthRemoteServiceConsts.ModuleName)]
[RemoteService(Name = HealthRemoteServiceConsts.RemoteServiceName)]
[Route("api/Health/employee")]
public class EmployeeController : AbpControllerBase, IEmployeeAppService
{
private readonly IEmployeeAppService _employeeAppService;
public EmployeeController(IEmployeeAppService employeeAppService)
{
_employeeAppService = employeeAppService;
}
[HttpPost]
[Route("CreateWithUser")]
public Task<EmployeeDto> CreateWithUserAsync(CreateEmployeeWithUserInput input)
{
return _employeeAppService.CreateWithUserAsync(input);
}
[HttpDelete]
[Route("Delete")]
public Task DeleteAsync(Guid id)
{
return _employeeAppService.DeleteAsync(id);
}
[HttpPut]
[Route("UpdateWithUser")]
public Task<EmployeeDto> UpdateWithUserAsync(CreateEmployeeInput input)
{
return _employeeAppService.UpdateWithUserAsync(input);
}
[HttpGet]
[Route("Get")]
public Task<EmployeeDto> GetAsync(Guid id)
{
return _employeeAppService.GetAsync(id);
}
[HttpGet]
[Route("GetAll")]
public Task<PagedResultDto<EmployeeDto>> GetAllAsync(GetAllEmployeeInput input)
{
return _employeeAppService.GetAllAsync(input);
}
}
執行專案
在Web端,進入組織機構
我們隨意建立幾個部門,如下圖所示:
建立幾個員工使用者,並將他們分配到「研發組A」,「研發組B」中,
通過點選不同的組織架構,可以檢視不同的使用者:
「研發組A」中的使用者:
「研發組B」中的使用者:
「未分配」中的使用者:
在某個組別中點選「篩選」,選擇職稱-中級,點選查詢
將查詢所有職稱為中級的員工
組合查詢的報文Payload如下圖:
下一章,我們將實現通用查詢應用層基礎類別,使按組織查詢賦能到所有的業務實體上。
本文來自部落格園,作者:林曉lx,轉載請註明原文連結:https://www.cnblogs.com/jevonsflash/p/17548606.html