add support for Dynamic Entity Registration in DbContext by Provides IXEntityRegisterar interface and XBaseEntityRegisterar as an abstract Implementation ...
all other requirements handled inside Module.
This commit is contained in:
+109
-6
@@ -1,5 +1,10 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using xCommons.Extensions;
|
||||
using xDataService.Configuration;
|
||||
using xDataService.Helpers;
|
||||
using xDataService.Providers;
|
||||
|
||||
namespace xDataService.Db
|
||||
{
|
||||
@@ -9,7 +14,13 @@ namespace xDataService.Db
|
||||
/// </summary>
|
||||
public abstract class XDbContext : DbContext
|
||||
{
|
||||
//
|
||||
#region Properties ...
|
||||
private readonly string database;
|
||||
private readonly XDataServiceConfiguration config;
|
||||
private readonly string[] dynamicEntityRegisterarsNames;
|
||||
private readonly XEntityRegisterarHandlerService dynamicEntityHandlers;
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Entities ...
|
||||
@@ -18,10 +29,13 @@ namespace xDataService.Db
|
||||
#endregion
|
||||
|
||||
//
|
||||
// Constructor ...
|
||||
#region Constructor ...
|
||||
public XDbContext(
|
||||
string database,
|
||||
DbContextOptions options,
|
||||
XDataServiceConfiguration config
|
||||
XDataServiceConfiguration config,
|
||||
XEntityRegisterarHandlerService dynamicEntityHandlers,
|
||||
string[] dynamicEntityRegisterarsNames = null
|
||||
) : base(options)
|
||||
{
|
||||
//
|
||||
@@ -32,11 +46,62 @@ namespace xDataService.Db
|
||||
}
|
||||
catch { }
|
||||
|
||||
//
|
||||
this.config = config;
|
||||
this.database = database;
|
||||
this.dynamicEntityHandlers = dynamicEntityHandlers;
|
||||
this.dynamicEntityRegisterarsNames = dynamicEntityRegisterarsNames;
|
||||
|
||||
//
|
||||
// Handling DbSets of Dynamic Entities ...
|
||||
if (!dynamicEntityHandlers.IsNull())
|
||||
{
|
||||
//
|
||||
// Configure XEntityRegisterarHandlerService ...
|
||||
dynamicEntityHandlers.HandleRegisteredEntities();
|
||||
|
||||
//
|
||||
// Loop through Entity Types ...
|
||||
foreach (var entityType in dynamicEntityHandlers.GetEntityTypes(dynamicEntityRegisterarsNames))
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Use reflection to call Set<T>()
|
||||
var method = typeof(XDbContext)
|
||||
.GetMethod(nameof(XDbContext.Set), Type.EmptyTypes)
|
||||
.MakeGenericMethod(entityType);
|
||||
if (!method.IsNull())
|
||||
{
|
||||
method.Invoke(this, null);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Extract Current Database Configuration ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public XDataBaseConfiguration GetDatabaseConfiguration()
|
||||
{
|
||||
//
|
||||
XDataBaseConfiguration result =
|
||||
config.Databases.FirstOrDefault(db => string
|
||||
.Equals(db.Key, database, StringComparison.InvariantCultureIgnoreCase)).Value;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Fluent API ...
|
||||
#region Overrides ...
|
||||
//
|
||||
// Prepare Model Configs ...
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
//
|
||||
@@ -46,15 +111,37 @@ namespace xDataService.Db
|
||||
// Automatically apply all IEntityTypeConfiguration<T> in this assembly
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(XDbContext).Assembly);
|
||||
|
||||
//
|
||||
// Extract Entity Container Assemblies ...
|
||||
var entityAssemblies = XDataServiceHelper.ExtractEntityAssemblies();
|
||||
foreach(var assembly in entityAssemblies)
|
||||
{
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(assembly);
|
||||
}
|
||||
|
||||
//
|
||||
// Check Dynamic Registered Service is Exists or not ...
|
||||
if (!dynamicEntityHandlers.IsNull())
|
||||
{
|
||||
//
|
||||
// Set Entity of Dynamically Registered Entities ...
|
||||
foreach (var entityType in dynamicEntityHandlers.GetEntityTypes(dynamicEntityRegisterarsNames))
|
||||
{
|
||||
modelBuilder.Entity(entityType);
|
||||
}
|
||||
|
||||
//
|
||||
// Configure Entity using Provided Registerar ...
|
||||
dynamicEntityHandlers
|
||||
.HandleApplyRegisteredEntitiesConfigurations(modelBuilder, dynamicEntityRegisterarsNames);
|
||||
}
|
||||
|
||||
//
|
||||
// Since this class inherit from Identity Db Context,
|
||||
// we have to pass this to Building Identity Models ...
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
|
||||
//
|
||||
public abstract void OnXModelCreating(ModelBuilder modelBuilder);
|
||||
|
||||
//
|
||||
// Prepare Configuring ...
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
@@ -87,8 +174,24 @@ namespace xDataService.Db
|
||||
//
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Abstracts ...
|
||||
//
|
||||
public abstract void OnXModelCreating(ModelBuilder modelBuilder);
|
||||
|
||||
//
|
||||
public abstract void OnXConfiguring(DbContextOptionsBuilder optionsBuilder);
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Dynamic DbSet(s) ...
|
||||
public override DbSet<TEntity> Set<TEntity>()
|
||||
where TEntity : class
|
||||
{
|
||||
return base.Set<TEntity>();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user