Added basic configuration
This commit is contained in:
37
src/HopFrame.Core/Config/DbContextConfig.cs
Normal file
37
src/HopFrame.Core/Config/DbContextConfig.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
public class DbContextConfig {
|
||||
public Type ContextType { get; }
|
||||
public List<TableConfig> Tables { get; init; } = new();
|
||||
|
||||
public DbContextConfig(Type context) {
|
||||
ContextType = context;
|
||||
|
||||
foreach (var property in ContextType.GetProperties()) {
|
||||
if (!property.PropertyType.IsGenericType) continue;
|
||||
var innerType = property.PropertyType.GenericTypeArguments.First();
|
||||
var setType = typeof(DbSet<>).MakeGenericType(innerType);
|
||||
if (property.PropertyType != setType) continue;
|
||||
|
||||
var table = new TableConfig(this, innerType, property.Name);
|
||||
Tables.Add(table);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class DbContextConfig<TDbContext>(Type context) : DbContextConfig(context) where TDbContext : DbContext {
|
||||
|
||||
public DbContextConfig<TDbContext> Table<TModel>(Action<TableConfig<TModel>> configurator) where TModel : class {
|
||||
var table = Table<TModel>();
|
||||
configurator.Invoke(table);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableConfig<TModel> Table<TModel>() where TModel : class {
|
||||
var table = Tables.Single(table => table.TableType == typeof(TModel));
|
||||
return new TableConfig<TModel>(table);
|
||||
}
|
||||
|
||||
}
|
||||
27
src/HopFrame.Core/Config/HopFrameConfig.cs
Normal file
27
src/HopFrame.Core/Config/HopFrameConfig.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
public class HopFrameConfig {
|
||||
public List<DbContextConfig> Contexts { get; init; } = new();
|
||||
public bool DisplayUserInfo { get; set; } = true;
|
||||
}
|
||||
|
||||
public class HopFrameConfigurator(HopFrameConfig config) {
|
||||
public HopFrameConfigurator AddDbContext<TDbContext>(Action<DbContextConfig<TDbContext>> configurator) where TDbContext : DbContext {
|
||||
var context = AddDbContext<TDbContext>();
|
||||
configurator.Invoke(context);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DbContextConfig<TDbContext> AddDbContext<TDbContext>() where TDbContext : DbContext {
|
||||
var context = new DbContextConfig<TDbContext>(typeof(TDbContext));
|
||||
config.Contexts.Add(context);
|
||||
return context;
|
||||
}
|
||||
|
||||
public HopFrameConfigurator DisplayUserInfo(bool display) {
|
||||
config.DisplayUserInfo = display;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
17
src/HopFrame.Core/Config/TableConfig.cs
Normal file
17
src/HopFrame.Core/Config/TableConfig.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
public class TableConfig(DbContextConfig config, Type tableType, string propertyName) {
|
||||
public Type TableType { get; } = tableType;
|
||||
public string PropertyName { get; } = propertyName;
|
||||
public DbContextConfig ContextConfig { get; } = config;
|
||||
public bool Ignored { get; set; }
|
||||
}
|
||||
|
||||
public class TableConfig<TModel>(TableConfig innerConfig) {
|
||||
|
||||
public TableConfig<TModel> Ignore() {
|
||||
innerConfig.Ignored = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user