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.
This commit is contained in:
Sipke Schoorstra 2024-05-28 21:12:45 +02:00
parent ad236f626f
commit f162186a5f

View file

@ -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;
/// <summary>
/// An optional base class to implement with some opinions on certain converters to install for certain DB providers.
/// </summary>
@ -12,10 +13,11 @@ public abstract class ElsaDbContextBase : DbContext, IElsaDbContextSchema
/// <summary>
/// The default schema used by Elsa.
/// </summary>
public static string ElsaSchema { get; set; } = "Elsa";
private string _schema;
/// <inheritdoc/>
public string Schema => _schema;
public static string ElsaSchema { get; set; } = "Elsa";
/// <inheritdoc/>
public string Schema { get; }
/// <summary>
/// The table used to store the migrations history.
/// </summary>
@ -26,16 +28,11 @@ public abstract class ElsaDbContextBase : DbContext, IElsaDbContextSchema
/// </summary>
protected ElsaDbContextBase(DbContextOptions options) : base(options)
{
var elsaDbContextOptions = options.FindExtension<ElsaDbContextOptionsExtension>()?.Options;
// ReSharper disable once VirtualMemberCallInConstructor
_schema = !string.IsNullOrWhiteSpace(elsaDbContextOptions?.SchemaName) ? elsaDbContextOptions.SchemaName : ElsaSchema;
}
var elsaDbContextOptions = options.FindExtension<ElsaDbContextOptionsExtension>()?.Options;
/// <summary>
/// The schema used by Elsa.
/// </summary>
// protected virtual string Schema { get; set; }
// ReSharper disable once VirtualMemberCallInConstructor
Schema = !string.IsNullOrWhiteSpace(elsaDbContextOptions?.SchemaName) ? elsaDbContextOptions.SchemaName : ElsaSchema;
}
/// <inheritdoc />
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);
}
/// <summary>