pull/8897/head
liangshiwei 5 years ago
parent adc3097dd7
commit d13b6c8b0c

@ -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<TDbContext>(
this IServiceCollection services,
this IServiceCollection services,
Action<IAbpDbContextRegistrationOptionsBuilder> optionsBuilder = null)
where TDbContext : AbpDbContext<TDbContext>
{
@ -19,6 +21,12 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddTransient(DbContextOptionsFactory.Create<TDbContext>);
var replaceDbContextAttribute = typeof(TDbContext).GetCustomAttribute<ReplaceDbContextAttribute>(true);
if (replaceDbContextAttribute != null)
{
options.ReplacedDbContextTypes.AddRange(replaceDbContextAttribute.ReplacedDbContextTypes);
}
foreach (var dbContextType in options.ReplacedDbContextTypes)
{
services.Replace(

@ -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<AbpDbContextOptions>(options =>

@ -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<ReplaceDbContextAttribute>(true);
if (replaceDbContextAttribute == null)
{
return;
}
foreach (var dbContextType in replaceDbContextAttribute.ReplacedDbContextTypes)
{
services.Replace(
ServiceDescriptor.Transient(
dbContextType,
sp => sp.GetRequiredService(type)
)
);
services.Configure<AbpDbContextOptions>(opts => { opts.DbContextReplacements[dbContextType] = type; });
}
}
}
}

@ -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<ReplaceDbContextAttribute>(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(

@ -24,27 +24,6 @@ namespace Volo.Abp.MongoDB.DependencyInjection
}
services.Add(ServiceDescriptor.Describe(typeof(IAbpMongoDbContext), type, ServiceLifetime.Transient));
var replaceDbContextAttribute = type.GetCustomAttribute<ReplaceDbContextAttribute>(true);
if (replaceDbContextAttribute == null)
{
return;
}
foreach (var dbContextType in replaceDbContextAttribute.ReplacedDbContextTypes)
{
services.Replace(
ServiceDescriptor.Transient(
dbContextType,
sp => sp.GetRequiredService(type)
)
);
services.Configure<AbpMongoDbContextOptions>(opts =>
{
opts.DbContextReplacements[dbContextType] = type;
});
}
}
}
}

Loading…
Cancel
Save