Browse Source

合并

master
2017875139 3 years ago
parent
commit
09f11ae959
  1. 4
      sample/Tenant.Api/DDD/Contracts/TenantManagementMappingProfile.cs
  2. 8
      sample/Tenant.Api/DDD/Domain/TenantAggregation/ITenantManager.cs
  3. 56
      sample/Tenant.Api/DDD/Domain/TenantAggregation/Tenant.cs
  4. 36
      sample/Tenant.Api/DDD/Domain/TenantAggregation/TenantConnectionString.cs
  5. 44
      sample/Tenant.Api/DDD/Domain/TenantAggregation/TenantManager.cs
  6. 13
      sample/Tenant.Api/TenantMappingProfile.cs

4
sample/Tenant.Api/DDD/Contracts/TenantManagementDomainMappingProfile.cs → sample/Tenant.Api/DDD/Contracts/TenantManagementMappingProfile.cs

@ -5,9 +5,9 @@ using TenantManagement.Api.DDD.Domain.Entites;
namespace TenantManagement.Api.DDD.Contracts;
public class TenantManagementDomainMappingProfile : Profile
public class TenantManagementMappingProfile : Profile
{
public TenantManagementDomainMappingProfile()
public TenantManagementMappingProfile()
{
CreateMap<TenantCreateInput, Tenant>();
CreateMap<TenantUpdateInput, Tenant>();

8
sample/Tenant.Api/DDD/Domain/TenantAggregation/ITenantManager.cs

@ -1,8 +0,0 @@
namespace Tenant.Api.DDD.Domain.TenantAggregate;
public interface ITenantManager
{
Task<Tenant> CreateAsync(string name);
Task ChangeNameAsync(Tenant tenant, string name);
}

56
sample/Tenant.Api/DDD/Domain/TenantAggregation/Tenant.cs

@ -1,56 +0,0 @@
using Easy;
using Easy.DDD.Domain.Entities;
using Tenant.Api.DDD.Domain.Consts;
namespace Tenant.Api.DDD.Domain.TenantAggregation;
public class Tenant : AggregateRoot<Guid>
{
public virtual string Name { get; protected set; }
public virtual List<TenantConnectionString> ConnectionStrings { get; protected set; }
protected Tenant()
{
}
protected internal Tenant(Guid id, string name)
: base(id)
{
SetName(name);
ConnectionStrings = new List<TenantConnectionString>();
}
public virtual void SetConnectionString(string name, string connectionString)
{
var tenantConnectionString = ConnectionStrings.FirstOrDefault(x => x.Name == name);
if (tenantConnectionString != null)
{
tenantConnectionString.SetValue(connectionString);
}
else
{
ConnectionStrings.Add(new TenantConnectionString(Id, name, connectionString));
}
}
public virtual void RemoveConnectionString(string name)
{
var tenantConnectionString = ConnectionStrings.FirstOrDefault(x => x.Name == name);
if (tenantConnectionString != null)
{
ConnectionStrings.Remove(tenantConnectionString);
}
}
protected internal virtual void SetName(string name)
{
Name = Check.NotNullOrWhiteSpace(name, nameof(name), TenantConsts.MaxNameLength);
}
}

36
sample/Tenant.Api/DDD/Domain/TenantAggregation/TenantConnectionString.cs

@ -1,36 +0,0 @@
using Easy;
using Easy.DDD.Domain.Entities;
using Tenant.Api.DDD.Domain.Consts;
namespace Tenant.Api.DDD.Domain.TenantAggregation;
public class TenantConnectionString : Entity
{
public virtual Guid TenantId { get; protected set; }
public virtual string Name { get; protected set; }
public virtual string Value { get; protected set; }
protected TenantConnectionString()
{
}
public TenantConnectionString(Guid tenantId, string name, string value)
{
TenantId = tenantId;
Name = Check.NotNullOrWhiteSpace(name, nameof(name), TenantConnectionStringConsts.MaxNameLength);
SetValue(value);
}
public virtual void SetValue(string value)
{
Value = Check.NotNullOrWhiteSpace(value, nameof(value), TenantConnectionStringConsts.MaxValueLength);
}
public override object[] GetKeys()
{
return new object[] { TenantId, Name };
}
}

44
sample/Tenant.Api/DDD/Domain/TenantAggregation/TenantManager.cs

@ -1,44 +0,0 @@
using Easy;
using Easy.DDD.Domain.Repositories;
using Easy.Guids;
using Microsoft.EntityFrameworkCore;
namespace Tenant.Api.DDD.Domain.TenantAggregation;
public class TenantManager
{
public IRepository<Tenant> TenantRepository { get; }
protected IGuidGenerator GuidGenerator { get; }
public TenantManager(IRepository<Tenant> tenantRepository)
{
TenantRepository = tenantRepository;
}
public virtual async Task<Tenant> 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)
{
Check.NotNull(tenant, nameof(tenant));
Check.NotNull(name, nameof(name));
await ValidateNameAsync(name, tenant.Id);
tenant.SetName(name);
}
protected virtual async Task ValidateNameAsync(string name, Guid? expectedId = null)
{
var tenant = await TenantRepository
.Where(o => o.Name == name)
.Include(x => x.ConnectionStrings)
.OrderBy(t => t.Id)
.FirstOrDefaultAsync();
When.Is(tenant != null && tenant.Id != expectedId, "重复的租户名称: " + name);
}
}

13
sample/Tenant.Api/TenantMappingProfile.cs

@ -1,13 +0,0 @@
using AutoMapper;
using Tenant.Api.DDD.Domain.Shared;
namespace Tenant.Api.DDD.Domain;
public class TenantMappingProfile : Profile
{
public TenantMappingProfile()
{
CreateMap<TenantAggregate.Tenant, TenantEto>();
}
}
Loading…
Cancel
Save