Browse Source

Support preconfigure for dbcontextes.

pull/272/head
Halil İbrahim Kalkan 9 years ago
parent
commit
398af0b86b
  1. 27
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs
  2. 44
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/DbContextOptionsFactory.cs

27
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs

@ -7,15 +7,28 @@ namespace Volo.Abp.EntityFrameworkCore
{
public class AbpDbContextOptions
{
internal List<Action<AbpDbContextConfigurationContext>> DefaultPreConfigureActions { get; set; }
internal Action<AbpDbContextConfigurationContext> DefaultConfigureAction { get; set; }
internal Dictionary<Type, List<object>> PreConfigureActions { get; set; }
internal Dictionary<Type, object> ConfigureActions { get; set; }
public AbpDbContextOptions()
{
DefaultPreConfigureActions = new List<Action<AbpDbContextConfigurationContext>>();
PreConfigureActions = new Dictionary<Type, List<object>>();
ConfigureActions = new Dictionary<Type, object>();
}
public void PreConfigure([NotNull] Action<AbpDbContextConfigurationContext> action)
{
Check.NotNull(action, nameof(action));
DefaultPreConfigureActions.Add(action);
}
public void Configure([NotNull] Action<AbpDbContextConfigurationContext> action)
{
Check.NotNull(action, nameof(action));
@ -23,6 +36,20 @@ namespace Volo.Abp.EntityFrameworkCore
DefaultConfigureAction = action;
}
public void PreConfigure<TDbContext>([NotNull] Action<AbpDbContextConfigurationContext<TDbContext>> action)
where TDbContext : AbpDbContext<TDbContext>
{
Check.NotNull(action, nameof(action));
var actions = PreConfigureActions.GetOrDefault(typeof(TDbContext));
if (actions == null)
{
PreConfigureActions[typeof(TDbContext)] = actions = new List<object>();
}
actions.Add(action);
}
public void Configure<TDbContext>([NotNull] Action<AbpDbContextConfigurationContext<TDbContext>> action)
where TDbContext : AbpDbContext<TDbContext>
{

44
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/DbContextOptionsFactory.cs

@ -21,23 +21,53 @@ namespace Volo.Abp.EntityFrameworkCore.DependencyInjection
creationContext.ExistingConnection
);
var dbContextOptions = GetDbContextOptions<TDbContext>(serviceProvider);
var options = GetDbContextOptions<TDbContext>(serviceProvider);
var configureAction = dbContextOptions.ConfigureActions.GetOrDefault(typeof(TDbContext));
PreConfigure(options, context);
Configure(options, context);
return context.DbContextOptions.Options;
}
private static void PreConfigure<TDbContext>(
AbpDbContextOptions options,
AbpDbContextConfigurationContext<TDbContext> context)
where TDbContext : AbpDbContext<TDbContext>
{
foreach (var defaultPreConfigureAction in options.DefaultPreConfigureActions)
{
defaultPreConfigureAction.Invoke(context);
}
var preConfigureActions = options.PreConfigureActions.GetOrDefault(typeof(TDbContext));
if (!preConfigureActions.IsNullOrEmpty())
{
foreach (var preConfigureAction in preConfigureActions)
{
((Action<AbpDbContextConfigurationContext<TDbContext>>)preConfigureAction).Invoke(context);
}
}
}
private static void Configure<TDbContext>(
AbpDbContextOptions options,
AbpDbContextConfigurationContext<TDbContext> context)
where TDbContext : AbpDbContext<TDbContext>
{
var configureAction = options.ConfigureActions.GetOrDefault(typeof(TDbContext));
if (configureAction != null)
{
((Action<AbpDbContextConfigurationContext<TDbContext>>)configureAction).Invoke(context);
}
else if (dbContextOptions.DefaultConfigureAction != null)
else if (options.DefaultConfigureAction != null)
{
dbContextOptions.DefaultConfigureAction.Invoke(context);
options.DefaultConfigureAction.Invoke(context);
}
else
{
throw new AbpException($"No configuration found for {typeof(DbContext).AssemblyQualifiedName}! Use services.Configure<AbpDbContextOptions>(...) to configure it.");
throw new AbpException(
$"No configuration found for {typeof(DbContext).AssemblyQualifiedName}! Use services.Configure<AbpDbContextOptions>(...) to configure it.");
}
return context.DbContextOptions.Options;
}
private static AbpDbContextOptions GetDbContextOptions<TDbContext>(IServiceProvider serviceProvider)

Loading…
Cancel
Save