Added basic configuration

This commit is contained in:
2025-01-14 11:34:57 +01:00
parent e093d01c6a
commit f0bc9e23b8
30 changed files with 860 additions and 5 deletions

View 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);
}
}

View 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;
}
}

View 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;
}
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,14 @@
using HopFrame.Core.Services;
using HopFrame.Core.Services.Implementations;
using Microsoft.Extensions.DependencyInjection;
namespace HopFrame.Core;
public static class ServiceCollectionExtensions {
public static IServiceCollection AddHopFrameServices(this IServiceCollection services) {
services.AddTransient<IContextExplorer, ContextExplorer>();
return services;
}
}

View File

@@ -0,0 +1,8 @@
using HopFrame.Core.Config;
namespace HopFrame.Core.Services;
public interface IContextExplorer {
public IEnumerable<string> GetTableNames();
public TableConfig? GetTable(string name);
}

View File

@@ -0,0 +1,24 @@
using HopFrame.Core.Config;
namespace HopFrame.Core.Services.Implementations;
internal sealed class ContextExplorer(HopFrameConfig config) : IContextExplorer {
public IEnumerable<string> GetTableNames() {
foreach (var context in config.Contexts) {
foreach (var table in context.Tables) {
if (table.Ignored) continue;
yield return table.PropertyName;
}
}
}
public TableConfig? GetTable(string name) {
foreach (var context in config.Contexts) {
var table = context.Tables.FirstOrDefault(table => table.PropertyName == name);
if (table is not null)
return table;
}
return null;
}
}

View File

@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<SupportedPlatform Include="browser"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="9.0.0"/>
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.11.2" />
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.11.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HopFrame.Core\HopFrame.Core.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,19 @@
using HopFrame.Core;
using HopFrame.Core.Config;
using Microsoft.Extensions.DependencyInjection;
namespace HopFrame.Web;
public static class ServiceCollectionExtensions {
public static IServiceCollection AddHopFrame(this IServiceCollection services, Action<HopFrameConfigurator> configurator) {
var config = new HopFrameConfig();
configurator.Invoke(new HopFrameConfigurator(config));
services.AddSingleton(config);
services.AddHopFrameServices();
return services;
}
}

View File

@@ -0,0 +1,10 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.FluentUI.AspNetCore.Components
@using Icons = Microsoft.FluentUI.AspNetCore.Components.Icons
@using HopFrame.Web