committed by
GitHub
6 changed files with 232 additions and 0 deletions
@ -0,0 +1,50 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events.Distributed; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.Abp.TenantManagement; |
|||
|
|||
namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed |
|||
{ |
|||
public class TenantCreateEventHandler : IDistributedEventHandler<EntityCreatedEto<TenantEto>>, ITransientDependency |
|||
{ |
|||
private readonly IDataFilter _dataFilter; |
|||
private readonly ITenantRepository _tenantRepository; |
|||
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
|||
|
|||
public TenantCreateEventHandler( |
|||
IDataFilter dataFilter, |
|||
ITenantRepository tenantRepository, |
|||
IDistributedCache<TenantConfigurationCacheItem> cache) |
|||
{ |
|||
_cache = cache; |
|||
_dataFilter = dataFilter; |
|||
_tenantRepository = tenantRepository; |
|||
} |
|||
|
|||
public virtual async Task HandleEventAsync(EntityCreatedEto<TenantEto> eventData) |
|||
{ |
|||
// 禁用租户过滤器
|
|||
using (_dataFilter.Disable<IMultiTenant>()) |
|||
{ |
|||
var tenant = await _tenantRepository.FindAsync(eventData.Entity.Id, true); |
|||
if (tenant == null) |
|||
{ |
|||
return; |
|||
} |
|||
var connectionStrings = new ConnectionStrings(); |
|||
foreach (var tenantConnectionString in tenant.ConnectionStrings) |
|||
{ |
|||
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; |
|||
} |
|||
var cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings); |
|||
|
|||
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); |
|||
await _cache.SetAsync(cacheKey, cacheItem); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events.Distributed; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.TenantManagement; |
|||
|
|||
namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed |
|||
{ |
|||
public class TenantDeleteEventHandler : IDistributedEventHandler<EntityDeletedEto<TenantEto>>, ITransientDependency |
|||
{ |
|||
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
|||
|
|||
public TenantDeleteEventHandler( |
|||
IDistributedCache<TenantConfigurationCacheItem> cache) |
|||
{ |
|||
_cache = cache; |
|||
} |
|||
|
|||
public virtual async Task HandleEventAsync(EntityDeletedEto<TenantEto> eventData) |
|||
{ |
|||
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); |
|||
await _cache.RemoveAsync(cacheKey); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events.Distributed; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.Abp.TenantManagement; |
|||
|
|||
namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed |
|||
{ |
|||
public class TenantUpdateEventHandler : IDistributedEventHandler<EntityUpdatedEto<TenantEto>>, ITransientDependency |
|||
{ |
|||
private readonly IDataFilter _dataFilter; |
|||
private readonly ITenantRepository _tenantRepository; |
|||
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
|||
|
|||
public TenantUpdateEventHandler( |
|||
IDataFilter dataFilter, |
|||
ITenantRepository tenantRepository, |
|||
IDistributedCache<TenantConfigurationCacheItem> cache) |
|||
{ |
|||
_cache = cache; |
|||
_dataFilter = dataFilter; |
|||
_tenantRepository = tenantRepository; |
|||
} |
|||
|
|||
public virtual async Task HandleEventAsync(EntityUpdatedEto<TenantEto> eventData) |
|||
{ |
|||
// 禁用租户过滤器
|
|||
using (_dataFilter.Disable<IMultiTenant>()) |
|||
{ |
|||
var tenant = await _tenantRepository.FindAsync(eventData.Entity.Id, true); |
|||
if (tenant == null) |
|||
{ |
|||
return; |
|||
} |
|||
var connectionStrings = new ConnectionStrings(); |
|||
foreach (var tenantConnectionString in tenant.ConnectionStrings) |
|||
{ |
|||
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; |
|||
} |
|||
var cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings); |
|||
|
|||
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); |
|||
await _cache.SetAsync(cacheKey, cacheItem); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
using LINGYUN.Abp.TenantManagement; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events.Distributed; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.TenantManagement; |
|||
|
|||
namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed |
|||
{ |
|||
public class TenantCreateEventHandler : IDistributedEventHandler<EntityCreatedEto<TenantEto>>, ITransientDependency |
|||
{ |
|||
private readonly ITenantAppService _tenantAppService; |
|||
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
|||
|
|||
public TenantCreateEventHandler( |
|||
ITenantAppService tenantAppService, |
|||
IDistributedCache<TenantConfigurationCacheItem> cache) |
|||
{ |
|||
_cache = cache; |
|||
_tenantAppService = tenantAppService; |
|||
} |
|||
|
|||
public virtual async Task HandleEventAsync(EntityCreatedEto<TenantEto> eventData) |
|||
{ |
|||
var tenantDto = await _tenantAppService.GetAsync(eventData.Entity.Id); |
|||
var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(eventData.Entity.Id); |
|||
var connectionStrings = new ConnectionStrings(); |
|||
foreach (var tenantConnectionString in tenantConnectionStringsDto.Items) |
|||
{ |
|||
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; |
|||
} |
|||
var cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings); |
|||
|
|||
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); |
|||
await _cache.SetAsync(cacheKey, cacheItem); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events.Distributed; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.TenantManagement; |
|||
|
|||
namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed |
|||
{ |
|||
public class TenantDeleteEventHandler : IDistributedEventHandler<EntityDeletedEto<TenantEto>>, ITransientDependency |
|||
{ |
|||
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
|||
|
|||
public TenantDeleteEventHandler( |
|||
IDistributedCache<TenantConfigurationCacheItem> cache) |
|||
{ |
|||
_cache = cache; |
|||
} |
|||
|
|||
public virtual async Task HandleEventAsync(EntityDeletedEto<TenantEto> eventData) |
|||
{ |
|||
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); |
|||
await _cache.RemoveAsync(cacheKey); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
using LINGYUN.Abp.TenantManagement; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events.Distributed; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.TenantManagement; |
|||
|
|||
namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed |
|||
{ |
|||
public class TenantUpdateEventHandler : IDistributedEventHandler<EntityUpdatedEto<TenantEto>>, ITransientDependency |
|||
{ |
|||
private readonly ITenantAppService _tenantAppService; |
|||
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
|||
|
|||
public TenantUpdateEventHandler( |
|||
ITenantAppService tenantAppService, |
|||
IDistributedCache<TenantConfigurationCacheItem> cache) |
|||
{ |
|||
_cache = cache; |
|||
_tenantAppService = tenantAppService; |
|||
} |
|||
|
|||
public virtual async Task HandleEventAsync(EntityUpdatedEto<TenantEto> eventData) |
|||
{ |
|||
var tenantDto = await _tenantAppService.GetAsync(eventData.Entity.Id); |
|||
var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(eventData.Entity.Id); |
|||
var connectionStrings = new ConnectionStrings(); |
|||
foreach (var tenantConnectionString in tenantConnectionStringsDto.Items) |
|||
{ |
|||
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; |
|||
} |
|||
var cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings); |
|||
|
|||
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); |
|||
await _cache.SetAsync(cacheKey, cacheItem); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue