From d13b6c8b0c9a2d9373f0db4a38c8b4369c943be1 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Fri, 7 May 2021 09:11:41 +0800 Subject: [PATCH] Improved --- .../AbpEfCoreServiceCollectionExtensions.cs | 10 ++++- .../AbpEntityFrameworkCoreModule.cs | 5 --- .../AbpDbContextConventionalRegistrar.cs | 37 ------------------- .../AbpMongoDbServiceCollectionExtensions.cs | 10 ++++- .../AbpMongoDbConventionalRegistrar.cs | 21 ----------- 5 files changed, 18 insertions(+), 65 deletions(-) delete mode 100644 framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextConventionalRegistrar.cs diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs index 538614ee22..07e86cb0e3 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs @@ -1,5 +1,7 @@ using System; +using System.Reflection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Volo.Abp.DependencyInjection; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.DependencyInjection; @@ -8,7 +10,7 @@ namespace Microsoft.Extensions.DependencyInjection public static class AbpEfCoreServiceCollectionExtensions { public static IServiceCollection AddAbpDbContext( - this IServiceCollection services, + this IServiceCollection services, Action optionsBuilder = null) where TDbContext : AbpDbContext { @@ -19,6 +21,12 @@ namespace Microsoft.Extensions.DependencyInjection services.TryAddTransient(DbContextOptionsFactory.Create); + var replaceDbContextAttribute = typeof(TDbContext).GetCustomAttribute(true); + if (replaceDbContextAttribute != null) + { + options.ReplacedDbContextTypes.AddRange(replaceDbContextAttribute.ReplacedDbContextTypes); + } + foreach (var dbContextType in options.ReplacedDbContextTypes) { services.Replace( diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs index 3d03698443..b5d9eb8436 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreModule.cs @@ -11,11 +11,6 @@ namespace Volo.Abp.EntityFrameworkCore [DependsOn(typeof(AbpDddDomainModule))] public class AbpEntityFrameworkCoreModule : AbpModule { - public override void PreConfigureServices(ServiceConfigurationContext context) - { - context.Services.AddConventionalRegistrar(new AbpDbContextConventionalRegistrar()); - } - public override void ConfigureServices(ServiceConfigurationContext context) { Configure(options => diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextConventionalRegistrar.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextConventionalRegistrar.cs deleted file mode 100644 index d9d6214895..0000000000 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextConventionalRegistrar.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Reflection; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.EntityFrameworkCore.DependencyInjection -{ - public class AbpDbContextConventionalRegistrar : DefaultConventionalRegistrar - { - public override void AddType(IServiceCollection services, Type type) - { - if (!typeof(IAbpEfCoreDbContext).IsAssignableFrom(type) || type == typeof(AbpDbContext<>)) - { - return; - } - - var replaceDbContextAttribute = type.GetCustomAttribute(true); - if (replaceDbContextAttribute == null) - { - return; - } - - foreach (var dbContextType in replaceDbContextAttribute.ReplacedDbContextTypes) - { - services.Replace( - ServiceDescriptor.Transient( - dbContextType, - sp => sp.GetRequiredService(type) - ) - ); - - services.Configure(opts => { opts.DbContextReplacements[dbContextType] = type; }); - } - } - } -} diff --git a/framework/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs b/framework/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs index 5adf7300ee..48e3115306 100644 --- a/framework/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs @@ -1,5 +1,7 @@ using System; +using System.Reflection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Volo.Abp.DependencyInjection; using Volo.Abp.MongoDB; using Volo.Abp.MongoDB.DependencyInjection; @@ -13,11 +15,17 @@ namespace Microsoft.Extensions.DependencyInjection var options = new AbpMongoDbContextRegistrationOptions(typeof(TMongoDbContext), services); optionsBuilder?.Invoke(options); + var replaceDbContextAttribute = typeof(TMongoDbContext).GetCustomAttribute(true); + if (replaceDbContextAttribute != null) + { + options.ReplacedDbContextTypes.AddRange(replaceDbContextAttribute.ReplacedDbContextTypes); + } + foreach (var dbContextType in options.ReplacedDbContextTypes) { services.Replace(ServiceDescriptor.Transient(dbContextType, typeof(TMongoDbContext))); } - + foreach (var dbContextType in options.ReplacedDbContextTypes) { services.Replace( diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/AbpMongoDbConventionalRegistrar.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/AbpMongoDbConventionalRegistrar.cs index 8e8a0e39aa..e0a6265f7c 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/AbpMongoDbConventionalRegistrar.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/AbpMongoDbConventionalRegistrar.cs @@ -24,27 +24,6 @@ namespace Volo.Abp.MongoDB.DependencyInjection } services.Add(ServiceDescriptor.Describe(typeof(IAbpMongoDbContext), type, ServiceLifetime.Transient)); - - var replaceDbContextAttribute = type.GetCustomAttribute(true); - if (replaceDbContextAttribute == null) - { - return; - } - - foreach (var dbContextType in replaceDbContextAttribute.ReplacedDbContextTypes) - { - services.Replace( - ServiceDescriptor.Transient( - dbContextType, - sp => sp.GetRequiredService(type) - ) - ); - - services.Configure(opts => - { - opts.DbContextReplacements[dbContextType] = type; - }); - } } } }