14 changed files with 218 additions and 307 deletions
@ -1,68 +0,0 @@ |
|||
using Microsoft.Extensions.Logging; |
|||
using System; |
|||
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; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed |
|||
{ |
|||
public class TenantCreateEventHandler : IDistributedEventHandler<EntityCreatedEto<TenantEto>>, ITransientDependency |
|||
{ |
|||
private readonly ILogger<TenantCreateEventHandler> _logger; |
|||
private readonly ICurrentTenant _currentTenant; |
|||
private readonly ITenantRepository _tenantRepository; |
|||
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
|||
|
|||
public TenantCreateEventHandler( |
|||
ICurrentTenant currentTenant, |
|||
ITenantRepository tenantRepository, |
|||
ILogger<TenantCreateEventHandler> logger, |
|||
IDistributedCache<TenantConfigurationCacheItem> cache) |
|||
{ |
|||
_cache = cache; |
|||
_logger = logger; |
|||
_currentTenant = currentTenant; |
|||
_tenantRepository = tenantRepository; |
|||
} |
|||
|
|||
[UnitOfWork] |
|||
public virtual async Task HandleEventAsync(EntityCreatedEto<TenantEto> eventData) |
|||
{ |
|||
try |
|||
{ |
|||
using (_currentTenant.Change(null)) |
|||
{ |
|||
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); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), |
|||
cacheItem); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), |
|||
cacheItem); |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogException(ex); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,29 +0,0 @@ |
|||
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) |
|||
{ |
|||
await _cache.RemoveAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString())); |
|||
|
|||
await _cache.RemoveAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,95 @@ |
|||
using Microsoft.Extensions.Logging; |
|||
using System; |
|||
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; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed; |
|||
|
|||
public class TenantSynchronizer : |
|||
IDistributedEventHandler<EntityCreatedEto<TenantEto>>, |
|||
IDistributedEventHandler<EntityUpdatedEto<TenantEto>>, |
|||
IDistributedEventHandler<EntityDeletedEto<TenantEto>>, |
|||
ITransientDependency |
|||
{ |
|||
private readonly ILogger<TenantSynchronizer> _logger; |
|||
private readonly ICurrentTenant _currentTenant; |
|||
private readonly ITenantRepository _tenantRepository; |
|||
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
|||
|
|||
public TenantSynchronizer( |
|||
ICurrentTenant currentTenant, |
|||
ITenantRepository tenantRepository, |
|||
ILogger<TenantSynchronizer> logger, |
|||
IDistributedCache<TenantConfigurationCacheItem> cache) |
|||
{ |
|||
_cache = cache; |
|||
_logger = logger; |
|||
_currentTenant = currentTenant; |
|||
_tenantRepository = tenantRepository; |
|||
} |
|||
|
|||
[UnitOfWork] |
|||
public virtual async Task HandleEventAsync(EntityUpdatedEto<TenantEto> eventData) |
|||
{ |
|||
await UpdateCacheItemAsync(eventData.Entity); |
|||
} |
|||
|
|||
[UnitOfWork] |
|||
public virtual async Task HandleEventAsync(EntityCreatedEto<TenantEto> eventData) |
|||
{ |
|||
await UpdateCacheItemAsync(eventData.Entity); |
|||
} |
|||
|
|||
public virtual async Task HandleEventAsync(EntityDeletedEto<TenantEto> eventData) |
|||
{ |
|||
using (_currentTenant.Change(null)) |
|||
{ |
|||
await _cache.RemoveManyAsync( |
|||
new string[] |
|||
{ |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), |
|||
}); |
|||
} |
|||
} |
|||
|
|||
protected virtual async Task UpdateCacheItemAsync(TenantEto tenant) |
|||
{ |
|||
try |
|||
{ |
|||
using (_currentTenant.Change(null)) |
|||
{ |
|||
var findTenant = await _tenantRepository.FindAsync(tenant.Id, true); |
|||
if (findTenant == null) |
|||
{ |
|||
return; |
|||
} |
|||
var connectionStrings = new ConnectionStrings(); |
|||
foreach (var tenantConnectionString in findTenant.ConnectionStrings) |
|||
{ |
|||
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; |
|||
} |
|||
var cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(findTenant.Id.ToString()), |
|||
cacheItem); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(findTenant.Name), |
|||
cacheItem); |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogException(ex); |
|||
} |
|||
} |
|||
} |
|||
@ -1,68 +0,0 @@ |
|||
using Microsoft.Extensions.Logging; |
|||
using System; |
|||
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; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed |
|||
{ |
|||
public class TenantUpdateEventHandler : IDistributedEventHandler<EntityUpdatedEto<TenantEto>>, ITransientDependency |
|||
{ |
|||
private readonly ILogger<TenantUpdateEventHandler> _logger; |
|||
private readonly ICurrentTenant _currentTenant; |
|||
private readonly ITenantRepository _tenantRepository; |
|||
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
|||
|
|||
public TenantUpdateEventHandler( |
|||
ICurrentTenant currentTenant, |
|||
ITenantRepository tenantRepository, |
|||
ILogger<TenantUpdateEventHandler> logger, |
|||
IDistributedCache<TenantConfigurationCacheItem> cache) |
|||
{ |
|||
_cache = cache; |
|||
_logger = logger; |
|||
_currentTenant = currentTenant; |
|||
_tenantRepository = tenantRepository; |
|||
} |
|||
|
|||
[UnitOfWork] |
|||
public virtual async Task HandleEventAsync(EntityUpdatedEto<TenantEto> eventData) |
|||
{ |
|||
try |
|||
{ |
|||
using (_currentTenant.Change(null)) |
|||
{ |
|||
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); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), |
|||
cacheItem); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), |
|||
cacheItem); |
|||
} |
|||
} |
|||
catch(Exception ex) |
|||
{ |
|||
_logger.LogException(ex); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,45 +0,0 @@ |
|||
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); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), |
|||
cacheItem); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), |
|||
cacheItem); |
|||
} |
|||
} |
|||
} |
|||
@ -1,29 +0,0 @@ |
|||
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) |
|||
{ |
|||
await _cache.RemoveAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString())); |
|||
|
|||
await _cache.RemoveAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,78 @@ |
|||
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.MultiTenancy; |
|||
using Volo.Abp.TenantManagement; |
|||
|
|||
namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed; |
|||
|
|||
public class TenantSynchronizer : |
|||
IDistributedEventHandler<EntityCreatedEto<TenantEto>>, |
|||
IDistributedEventHandler<EntityUpdatedEto<TenantEto>>, |
|||
IDistributedEventHandler<EntityDeletedEto<TenantEto>>, |
|||
ITransientDependency |
|||
{ |
|||
private readonly ICurrentTenant _currentTenant; |
|||
private readonly ITenantAppService _tenantAppService; |
|||
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; |
|||
|
|||
public TenantSynchronizer( |
|||
ICurrentTenant currentTenant, |
|||
ITenantAppService tenantAppService, |
|||
IDistributedCache<TenantConfigurationCacheItem> cache) |
|||
{ |
|||
_cache = cache; |
|||
_currentTenant = currentTenant; |
|||
_tenantAppService = tenantAppService; |
|||
} |
|||
|
|||
public virtual async Task HandleEventAsync(EntityUpdatedEto<TenantEto> eventData) |
|||
{ |
|||
await UpdateCacheItemAsync(eventData.Entity); |
|||
} |
|||
|
|||
public virtual async Task HandleEventAsync(EntityCreatedEto<TenantEto> eventData) |
|||
{ |
|||
await UpdateCacheItemAsync(eventData.Entity); |
|||
} |
|||
|
|||
public virtual async Task HandleEventAsync(EntityDeletedEto<TenantEto> eventData) |
|||
{ |
|||
using (_currentTenant.Change(null)) |
|||
{ |
|||
await _cache.RemoveManyAsync( |
|||
new string[] |
|||
{ |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), |
|||
}); |
|||
} |
|||
} |
|||
|
|||
protected virtual async Task UpdateCacheItemAsync(TenantEto tenant) |
|||
{ |
|||
using (_currentTenant.Change(null)) |
|||
{ |
|||
var tenantDto = await _tenantAppService.GetAsync(tenant.Id); |
|||
var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(tenant.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); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(tenant.Id.ToString()), |
|||
cacheItem); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(tenant.Name), |
|||
cacheItem); |
|||
} |
|||
} |
|||
} |
|||
@ -1,45 +0,0 @@ |
|||
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); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), |
|||
cacheItem); |
|||
|
|||
await _cache.SetAsync( |
|||
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), |
|||
cacheItem); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
{ |
|||
"profiles": { |
|||
"WSL": { |
|||
"commandName": "WSL2", |
|||
"distributionName": "" |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue