From 33fcd398a8c67f6728d9ae4cbc9216f089ab3a68 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 11 May 2021 09:48:39 +0800 Subject: [PATCH] Support multiple usages of the attribute --- .../AbpCommonDbContextRegistrationOptions.cs | 6 +++--- .../Abp/DependencyInjection/ReplaceDbContextAttribute.cs | 2 +- .../AbpEfCoreServiceCollectionExtensions.cs | 9 ++++++--- .../AbpMongoDbServiceCollectionExtensions.cs | 9 ++++++--- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/AbpCommonDbContextRegistrationOptions.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/AbpCommonDbContextRegistrationOptions.cs index 6408e3f0fa..ee172b3409 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/AbpCommonDbContextRegistrationOptions.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/AbpCommonDbContextRegistrationOptions.cs @@ -55,7 +55,7 @@ namespace Volo.Abp.DependencyInjection throw new AbpException($"{OriginalDbContextType.AssemblyQualifiedName} should inherit/implement {otherDbContextType.AssemblyQualifiedName}!"); } - ReplacedDbContextTypes.Add(otherDbContextType); + ReplacedDbContextTypes.AddIfNotContains(otherDbContextType); return this; } @@ -93,9 +93,9 @@ namespace Volo.Abp.DependencyInjection public IAbpCommonDbContextRegistrationOptionsBuilder AddDefaultRepository(Type entityType) { EntityHelper.CheckEntity(entityType); - + SpecifiedDefaultRepositories.AddIfNotContains(entityType); - + return this; } diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/ReplaceDbContextAttribute.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/ReplaceDbContextAttribute.cs index 708d3ff470..625e6841a0 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/ReplaceDbContextAttribute.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/ReplaceDbContextAttribute.cs @@ -2,7 +2,7 @@ namespace Volo.Abp.DependencyInjection { - [AttributeUsage(AttributeTargets.Class)] + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class ReplaceDbContextAttribute : Attribute { public Type[] ReplacedDbContextTypes { get; } 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 956b2937aa..a48575ddcc 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reflection; using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.DependencyInjection; @@ -18,10 +19,12 @@ namespace Microsoft.Extensions.DependencyInjection var options = new AbpDbContextRegistrationOptions(typeof(TDbContext), services); - var replaceDbContextAttribute = typeof(TDbContext).GetCustomAttribute(true); - if (replaceDbContextAttribute != null) + var replacedDbContextTypes = typeof(TDbContext).GetCustomAttributes(true) + .SelectMany( x => x.ReplacedDbContextTypes).ToList(); + + foreach (var dbContextType in replacedDbContextTypes) { - options.ReplacedDbContextTypes.AddRange(replaceDbContextAttribute.ReplacedDbContextTypes); + options.ReplaceDbContext(dbContextType); } optionsBuilder?.Invoke(options); 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 13131718c0..7f9c6b8d42 100644 --- a/framework/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reflection; using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.DependencyInjection; @@ -14,10 +15,12 @@ namespace Microsoft.Extensions.DependencyInjection { var options = new AbpMongoDbContextRegistrationOptions(typeof(TMongoDbContext), services); - var replaceDbContextAttribute = typeof(TMongoDbContext).GetCustomAttribute(true); - if (replaceDbContextAttribute != null) + var replacedDbContextTypes = typeof(TMongoDbContext).GetCustomAttributes(true) + .SelectMany( x => x.ReplacedDbContextTypes).ToList(); + + foreach (var dbContextType in replacedDbContextTypes) { - options.ReplacedDbContextTypes.AddRange(replaceDbContextAttribute.ReplacedDbContextTypes); + options.ReplaceDbContext(dbContextType); } optionsBuilder?.Invoke(options);