From f162186a5fb75e2f5a9fefb5ba448a8b5933091f Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Tue, 28 May 2024 21:12:45 +0200 Subject: [PATCH] Refactor ElsaDbContextBase to clean up schema setup The schema setup in ElsaDbContextBase has been refactored for better readability and more efficient coding. The '_schema' private variable has been removed, and the 'Schema' property is now set directly within the constructor. This eliminates the need for extra code to handle the '_schema' variable, simplifying the overall approach. Additionally, minor formatting adjustments were made for better consistency in the code block. --- .../ElsaDbContextBase.cs | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/modules/Elsa.EntityFrameworkCore.Common/ElsaDbContextBase.cs b/src/modules/Elsa.EntityFrameworkCore.Common/ElsaDbContextBase.cs index fe0f30ef6..da24d5a29 100644 --- a/src/modules/Elsa.EntityFrameworkCore.Common/ElsaDbContextBase.cs +++ b/src/modules/Elsa.EntityFrameworkCore.Common/ElsaDbContextBase.cs @@ -1,9 +1,10 @@ -using Elsa.EntityFrameworkCore.Common.Contracts; +using Elsa.EntityFrameworkCore.Common.Contracts; using Elsa.EntityFrameworkCore.Extensions; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Elsa.EntityFrameworkCore.Common; + /// /// An optional base class to implement with some opinions on certain converters to install for certain DB providers. /// @@ -12,10 +13,11 @@ public abstract class ElsaDbContextBase : DbContext, IElsaDbContextSchema /// /// The default schema used by Elsa. /// - public static string ElsaSchema { get; set; } = "Elsa"; - private string _schema; - /// - public string Schema => _schema; + public static string ElsaSchema { get; set; } = "Elsa"; + + /// + public string Schema { get; } + /// /// The table used to store the migrations history. /// @@ -26,16 +28,11 @@ public abstract class ElsaDbContextBase : DbContext, IElsaDbContextSchema /// protected ElsaDbContextBase(DbContextOptions options) : base(options) { - var elsaDbContextOptions = options.FindExtension()?.Options; - - // ReSharper disable once VirtualMemberCallInConstructor - _schema = !string.IsNullOrWhiteSpace(elsaDbContextOptions?.SchemaName) ? elsaDbContextOptions.SchemaName : ElsaSchema; - } + var elsaDbContextOptions = options.FindExtension()?.Options; - /// - /// The schema used by Elsa. - /// - // protected virtual string Schema { get; set; } + // ReSharper disable once VirtualMemberCallInConstructor + Schema = !string.IsNullOrWhiteSpace(elsaDbContextOptions?.SchemaName) ? elsaDbContextOptions.SchemaName : ElsaSchema; + } /// protected override void OnModelCreating(ModelBuilder modelBuilder) @@ -45,11 +42,11 @@ public abstract class ElsaDbContextBase : DbContext, IElsaDbContextSchema if (!Database.IsSqlite()) modelBuilder.HasDefaultSchema(Schema); } - + ApplyEntityConfigurations(modelBuilder); - if(Database.IsSqlite()) SetupForSqlite(modelBuilder); - if(Database.IsOracle()) SetupForOracle(modelBuilder); + if (Database.IsSqlite()) SetupForSqlite(modelBuilder); + if (Database.IsOracle()) SetupForOracle(modelBuilder); } ///