Browse Source

Resolved #9262: Allow to replace a dbcontext by a given type

pull/9276/head
Halil İbrahim Kalkan 5 years ago
parent
commit
d519600a2c
  1. 13
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/AbpCommonDbContextRegistrationOptions.cs
  2. 12
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/IAbpCommonDbContextRegistrationOptionsBuilder.cs
  3. 11
      framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs
  4. 12
      framework/src/Volo.Abp.MemoryDb/Microsoft/Extensions/DependencyInjection/AbpMemoryDbServiceCollectionExtensions.cs
  5. 16
      framework/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs

13
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/AbpCommonDbContextRegistrationOptions.cs

@ -15,7 +15,7 @@ namespace Volo.Abp.DependencyInjection
public IServiceCollection Services { get; }
public List<Type> ReplacedDbContextTypes { get; }
public Dictionary<Type, Type> ReplacedDbContextTypes { get; }
public Type DefaultRepositoryDbContextType { get; protected set; }
@ -39,7 +39,7 @@ namespace Volo.Abp.DependencyInjection
Services = services;
DefaultRepositoryDbContextType = originalDbContextType;
CustomRepositories = new Dictionary<Type, Type>();
ReplacedDbContextTypes = new List<Type>();
ReplacedDbContextTypes = new Dictionary<Type, Type>();
SpecifiedDefaultRepositories = new List<Type>();
}
@ -47,15 +47,20 @@ namespace Volo.Abp.DependencyInjection
{
return ReplaceDbContext(typeof(TOtherDbContext));
}
public IAbpCommonDbContextRegistrationOptionsBuilder ReplaceDbContext<TOtherDbContext, TTargetDbContext>()
{
return ReplaceDbContext(typeof(TOtherDbContext), typeof(TTargetDbContext));
}
public IAbpCommonDbContextRegistrationOptionsBuilder ReplaceDbContext(Type otherDbContextType)
public IAbpCommonDbContextRegistrationOptionsBuilder ReplaceDbContext(Type otherDbContextType, Type targetDbContextType = null)
{
if (!otherDbContextType.IsAssignableFrom(OriginalDbContextType))
{
throw new AbpException($"{OriginalDbContextType.AssemblyQualifiedName} should inherit/implement {otherDbContextType.AssemblyQualifiedName}!");
}
ReplacedDbContextTypes.AddIfNotContains(otherDbContextType);
ReplacedDbContextTypes[otherDbContextType] = targetDbContextType;
return this;
}

12
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/IAbpCommonDbContextRegistrationOptionsBuilder.cs

@ -75,9 +75,17 @@ namespace Volo.Abp.DependencyInjection
IAbpCommonDbContextRegistrationOptionsBuilder ReplaceDbContext<TOtherDbContext>();
/// <summary>
/// Replaces given DbContext type with this DbContext type.
/// Replaces given DbContext type with the target DbContext type.
/// </summary>
/// <typeparam name="TOtherDbContext">The DbContext type to be replaced</typeparam>
/// <typeparam name="TTargetDbContext">The target DbContext type</typeparam>
IAbpCommonDbContextRegistrationOptionsBuilder ReplaceDbContext<TOtherDbContext, TTargetDbContext>();
/// <summary>
/// Replaces given DbContext type with the given or this DbContext type.
/// </summary>
/// <param name="otherDbContextType">The DbContext type to be replaced</param>
IAbpCommonDbContextRegistrationOptionsBuilder ReplaceDbContext(Type otherDbContextType);
/// <param name="targetDbContextType">The target DbContext type (optional, used this DbContext type if not provided)</param>
IAbpCommonDbContextRegistrationOptionsBuilder ReplaceDbContext(Type otherDbContextType, Type targetDbContextType = null);
}
}

11
framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs

@ -31,18 +31,21 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddTransient(DbContextOptionsFactory.Create<TDbContext>);
foreach (var dbContextType in options.ReplacedDbContextTypes)
foreach (var entry in options.ReplacedDbContextTypes)
{
var originalDbContextType = entry.Key;
var targetDbContextType = entry.Value ?? typeof(TDbContext);
services.Replace(
ServiceDescriptor.Transient(
dbContextType,
sp => sp.GetRequiredService(typeof(TDbContext))
originalDbContextType,
sp => sp.GetRequiredService(targetDbContextType)
)
);
services.Configure<AbpDbContextOptions>(opts =>
{
opts.DbContextReplacements[dbContextType] = typeof(TDbContext);
opts.DbContextReplacements[originalDbContextType] = targetDbContextType;
});
}

12
framework/src/Volo.Abp.MemoryDb/Microsoft/Extensions/DependencyInjection/AbpMemoryDbServiceCollectionExtensions.cs

@ -18,9 +18,17 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddSingleton(options.DefaultRepositoryDbContextType, sp => sp.GetRequiredService<TMemoryDbContext>());
}
foreach (var dbContextType in options.ReplacedDbContextTypes)
foreach (var entry in options.ReplacedDbContextTypes)
{
services.Replace(ServiceDescriptor.Singleton(dbContextType, sp => sp.GetRequiredService<TMemoryDbContext>()));
var originalDbContextType = entry.Key;
var targetDbContextType = entry.Value ?? typeof(TMemoryDbContext);
services.Replace(
ServiceDescriptor.Singleton(
originalDbContextType,
sp => sp.GetRequiredService(targetDbContextType)
)
);
}
new MemoryDbRepositoryRegistrar(options).AddRepositories();

16
framework/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs

@ -25,23 +25,21 @@ namespace Microsoft.Extensions.DependencyInjection
optionsBuilder?.Invoke(options);
foreach (var dbContextType in options.ReplacedDbContextTypes)
{
services.Replace(ServiceDescriptor.Transient(dbContextType, typeof(TMongoDbContext)));
}
foreach (var dbContextType in options.ReplacedDbContextTypes)
foreach (var entry in options.ReplacedDbContextTypes)
{
var originalDbContextType = entry.Key;
var targetDbContextType = entry.Value ?? typeof(TMongoDbContext);
services.Replace(
ServiceDescriptor.Transient(
dbContextType,
sp => sp.GetRequiredService(typeof(TMongoDbContext))
originalDbContextType,
sp => sp.GetRequiredService(targetDbContextType)
)
);
services.Configure<AbpMongoDbContextOptions>(opts =>
{
opts.DbContextReplacements[dbContextType] = typeof(TMongoDbContext);
opts.DbContextReplacements[originalDbContextType] = targetDbContextType;
});
}

Loading…
Cancel
Save