Browse Source

Support multiple usages of the attribute

pull/8897/head
liangshiwei 5 years ago
parent
commit
33fcd398a8
  1. 6
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/AbpCommonDbContextRegistrationOptions.cs
  2. 2
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/ReplaceDbContextAttribute.cs
  3. 9
      framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs
  4. 9
      framework/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs

6
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;
}

2
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; }

9
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<ReplaceDbContextAttribute>(true);
if (replaceDbContextAttribute != null)
var replacedDbContextTypes = typeof(TDbContext).GetCustomAttributes<ReplaceDbContextAttribute>(true)
.SelectMany( x => x.ReplacedDbContextTypes).ToList();
foreach (var dbContextType in replacedDbContextTypes)
{
options.ReplacedDbContextTypes.AddRange(replaceDbContextAttribute.ReplacedDbContextTypes);
options.ReplaceDbContext(dbContextType);
}
optionsBuilder?.Invoke(options);

9
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<ReplaceDbContextAttribute>(true);
if (replaceDbContextAttribute != null)
var replacedDbContextTypes = typeof(TMongoDbContext).GetCustomAttributes<ReplaceDbContextAttribute>(true)
.SelectMany( x => x.ReplacedDbContextTypes).ToList();
foreach (var dbContextType in replacedDbContextTypes)
{
options.ReplacedDbContextTypes.AddRange(replaceDbContextAttribute.ReplacedDbContextTypes);
options.ReplaceDbContext(dbContextType);
}
optionsBuilder?.Invoke(options);

Loading…
Cancel
Save