diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/Migrations/MongoDatabaseMigrationEventHandlerBase.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/Migrations/MongoDatabaseMigrationEventHandlerBase.cs new file mode 100644 index 0000000000..36a86551c5 --- /dev/null +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/Migrations/MongoDatabaseMigrationEventHandlerBase.cs @@ -0,0 +1,58 @@ +using System; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.MongoDB.Migrations; + +public abstract class MongoDatabaseMigrationEventHandlerBase : + IDistributedEventHandler, + IDistributedEventHandler, + ITransientDependency + where TDbContext : IAbpMongoDbContext +{ + protected string DatabaseName { get; } + + + protected MongoDatabaseMigrationEventHandlerBase(string databaseName) + { + DatabaseName = databaseName; + } + + public virtual async Task HandleEventAsync(TenantCreatedEto eventData) + { + await SeedAsync(eventData.Id); + + await AfterTenantCreated(eventData); + } + + protected virtual Task AfterTenantCreated(TenantCreatedEto eventData) + { + return Task.CompletedTask; + } + + public virtual async Task HandleEventAsync(TenantConnectionStringUpdatedEto eventData) + { + if (eventData.ConnectionStringName != DatabaseName && + eventData.ConnectionStringName != Data.ConnectionStrings.DefaultConnectionStringName || + eventData.NewValue.IsNullOrWhiteSpace()) + { + return; + } + + await SeedAsync(eventData.Id); + + await AfterTenantConnectionStringUpdated(eventData); + } + + protected virtual Task AfterTenantConnectionStringUpdated(TenantConnectionStringUpdatedEto eventData) + { + return Task.CompletedTask; + } + + protected virtual Task SeedAsync(Guid? tenantId) + { + return Task.CompletedTask; + } +} \ No newline at end of file