using Microsoft.EntityFrameworkCore; namespace HopFrame.Core.Config; public class DbContextConfig { public Type ContextType { get; } public List Tables { get; } = new(); public HopFrameConfig ParentConfig { get; } public DbContextConfig(Type context, HopFrameConfig parentConfig) { ContextType = context; ParentConfig = parentConfig; 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.Count); Tables.Add(table); } } } /// /// A helper class for editing the /// public sealed class DbContextConfigurator(DbContextConfig config) { /// /// The Internal DbContext configuration that's modified by the helper functions /// public DbContextConfig InnerConfig { get; } = config; /// /// Configures the table of the DbContext using the provided configurator /// /// Used for configuring the table /// The model of the table for identifying the correct one /// public DbContextConfigurator Table(Action> configurator) where TModel : class { var table = Table(); configurator.Invoke(table); return this; } /// /// Configures the table of the DbContext /// /// The model of the table for identifying the correct one /// The configurator for the specified table /// public TableConfigurator Table() where TModel : class { var table = InnerConfig.Tables.Single(table => table.TableType == typeof(TModel)); return new TableConfigurator(table); } }