From 98e64b896bbf9d824e86b3181e644934632d5a76 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 8 Mar 2024 11:27:31 +0800 Subject: [PATCH] Update `AbpDbContextOptions`. --- .../Abp/EntityFrameworkCore/AbpDbContext.cs | 59 ++++-------- .../AbpDbContextOptions.cs | 92 ++++++++++--------- 2 files changed, 67 insertions(+), 84 deletions(-) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs index ef3e3b443c..e67280334c 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -117,29 +117,18 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, .Invoke(this, new object[] { modelBuilder, entityType }); } - if (LazyServiceProvider is null) + var abpDbContextOptions = LazyServiceProvider?.LazyGetRequiredService>().Value; + if (abpDbContextOptions != null) { - return; - } - var abpDbContextOptions = LazyServiceProvider.LazyGetRequiredService>().Value; + abpDbContextOptions.DefaultOnModelCreatingAction?.Invoke(this, modelBuilder); - var actions = abpDbContextOptions.ModelBuilderActions - .Where(x => x.Key == typeof(TDbContext) || x.Key == typeof(AbpDbContext<>)) - .SelectMany(x => x.Value) - .OrderBy(a => a.Key) - .Select(a => a.Value) - .ToList(); - - foreach (var action in actions) - { - if(action is Action modelBuilderAction) + var onModelCreatingActions = abpDbContextOptions.OnModelCreatingActions.GetOrDefault(typeof(TDbContext)); + if (!onModelCreatingActions.IsNullOrEmpty()) { - modelBuilderAction.Invoke(modelBuilder, this); - } - - if(this is TDbContext dbContext && action is Action dbContextAction) - { - dbContextAction.Invoke(modelBuilder, dbContext); + foreach (var onModelCreatingAction in onModelCreatingActions!) + { + onModelCreatingAction.As>().Invoke(this, modelBuilder); + } } } } @@ -148,30 +137,18 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, { base.ConfigureConventions(configurationBuilder); - if (LazyServiceProvider is null) + var abpDbContextOptions = LazyServiceProvider?.LazyGetRequiredService>().Value; + if (abpDbContextOptions != null) { - return; - } - - var abpDbContextOptions = LazyServiceProvider.LazyGetRequiredService>().Value; + abpDbContextOptions.DefaultConventionAction?.Invoke(this, configurationBuilder); - var actions = abpDbContextOptions.Conventions - .Where(x => x.Key == typeof(TDbContext) || x.Key == typeof(AbpDbContext<>)) - .SelectMany(x => x.Value) - .OrderBy(a => a.Key) - .Select(a => a.Value) - .ToList(); - - foreach (var action in actions) - { - if(action is Action modelBuilderAction) + var conventionActions = abpDbContextOptions.ConventionActions.GetOrDefault(typeof(TDbContext)); + if (!conventionActions.IsNullOrEmpty()) { - modelBuilderAction.Invoke(configurationBuilder, this); - } - - if(this is TDbContext dbContext && action is Action dbContextAction) - { - dbContextAction.Invoke(configurationBuilder, dbContext); + foreach (var conventionAction in conventionActions!) + { + conventionAction.As>().Invoke(this, configurationBuilder); + } } } } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs index f40974cdce..acde97340b 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs @@ -21,9 +21,13 @@ public class AbpDbContextOptions internal Dictionary DbContextReplacements { get; } - internal Dictionary>> Conventions { get; } - - internal Dictionary>> ModelBuilderActions { get; } + internal Action? DefaultConventionAction { get; set; } + + internal Dictionary> ConventionActions { get; } + + internal Action? DefaultOnModelCreatingAction { get; set; } + + internal Dictionary> OnModelCreatingActions { get; } public AbpDbContextOptions() { @@ -31,8 +35,8 @@ public class AbpDbContextOptions PreConfigureActions = new Dictionary>(); ConfigureActions = new Dictionary(); DbContextReplacements = new Dictionary(); - Conventions = new Dictionary>>(); - ModelBuilderActions = new Dictionary>>(); + ConventionActions = new Dictionary>(); + OnModelCreatingActions = new Dictionary>(); } public void PreConfigure([NotNull] Action action) @@ -48,27 +52,55 @@ public class AbpDbContextOptions DefaultConfigureAction = action; } - - public void ConfigureConventions([NotNull] Action action, Type? dbContextType = null, int? order = null) + + public void ConfigureDefaultConvention([NotNull] Action action) { - InternalConfigureConventions(action, dbContextType, order); + Check.NotNull(action, nameof(action)); + + DefaultConventionAction = action; } - - public void ConfigureConventions([NotNull] Action action, int? order = null) + + public void ConfigureConventions([NotNull] Action action) where TDbContext : AbpDbContext { - InternalConfigureConventions(action, typeof(TDbContext), order); + Check.NotNull(action, nameof(action)); + + var actions = ConventionActions.GetOrDefault(typeof(TDbContext)); + if (actions == null) + { + ConventionActions[typeof(TDbContext)] = new List + { + new Action((dbContext, builder) => action((TDbContext)dbContext, builder)) + }; + return; + } + + actions.Add(action); } - public void OnModelCreating([NotNull] Action action, Type? dbContextType = null, int? order = null) + public void ConfigureDefaultOnModelCreating([NotNull] Action action) { - InternalOnModelCreating(action, dbContextType, order); + Check.NotNull(action, nameof(action)); + + DefaultOnModelCreatingAction = action; } - - public void OnModelCreating([NotNull] Action action, int? order = null) + + public void ConfigureOnModelCreating([NotNull] Action action) where TDbContext : AbpDbContext { - InternalOnModelCreating(action, typeof(TDbContext), order); + Check.NotNull(action, nameof(action)); + + var actions = OnModelCreatingActions.GetOrDefault(typeof(TDbContext)); + if (actions == null) + { + OnModelCreatingActions[typeof(TDbContext)] = new List + { + new Action((dbContext, builder) => action((TDbContext)dbContext, builder)) + }; + return; + } + + actions.Add(action); } public bool IsConfiguredDefault() @@ -131,30 +163,4 @@ public class AbpDbContextOptions } } } - - private void InternalConfigureConventions(object action, Type? dbContextType = null, int? order = null) - { - Check.NotNull(action, nameof(action)); - - var actions = Conventions.GetOrDefault(dbContextType ?? typeof(AbpDbContext<>)); - if (actions == null) - { - Conventions[dbContextType ?? typeof(AbpDbContext<>)] = actions = new List>(); - } - - actions.Add(new KeyValuePair(order, action)); - } - - private void InternalOnModelCreating(object action, Type? dbContextType = null, int? order = null) - { - Check.NotNull(action, nameof(action)); - - var actions = ModelBuilderActions.GetOrDefault(dbContextType ?? typeof(AbpDbContext<>)); - if (actions == null) - { - ModelBuilderActions[dbContextType ?? typeof(AbpDbContext<>)] = actions = new List>(); - } - - actions.Add(new KeyValuePair(order, action)); - } -} \ No newline at end of file +}