using Easy; using Easy.DDD.Domain; using Easy.DDD.Domain.Repositories; using Microsoft.EntityFrameworkCore; using TenantManagement.Api.DDD.Domain.Entites; using TenantManagement.Api.DDD.DomainShared.IServices; namespace TenantManagement.Api.DDD.Domain.Services; public class TenantDomainService : DomainService, ITenantDomainService { protected IRepository TenantRepository { get; } public TenantDomainService(IRepository tenantRepository) { TenantRepository = tenantRepository; } public virtual async Task CreateAsync(string name) { Check.NotNull(name, nameof(name)); await ValidateNameAsync(name); return new Tenant(GuidGenerator.Create(), name); } public virtual async Task ChangeNameAsync(Tenant tenant, string name) { await ValidateNameAsync(name, tenant.Id); tenant.SetName(name); } protected virtual async Task ValidateNameAsync(string name, Guid? expectedId = null) { When.Is(string.IsNullOrEmpty(name),"租户名称不能为空"); var tenant = await TenantRepository.Where(o => o.TenantName == name).FirstOrDefaultAsync(); When.Is(tenant != null && tenant.Id != expectedId, "重复的租户名称: " + name); } }