Added modular event system

This commit is contained in:
2025-02-01 11:50:52 +01:00
parent 966ced57d6
commit 39641f18a8
22 changed files with 262 additions and 68 deletions

View File

@@ -1,13 +1,16 @@
using Microsoft.EntityFrameworkCore;
using HopFrame.Core.Events;
using Microsoft.EntityFrameworkCore;
namespace HopFrame.Core.Config;
public class DbContextConfig {
public Type ContextType { get; }
public List<TableConfig> Tables { get; } = new();
public HopFrameConfig ParentConfig { get; }
public DbContextConfig(Type context) {
public DbContextConfig(Type context, HopFrameConfig parentConfig) {
ContextType = context;
ParentConfig = parentConfig;
foreach (var property in ContextType.GetProperties()) {
if (!property.PropertyType.IsGenericType) continue;

View File

@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using HopFrame.Core.Events;
using Microsoft.EntityFrameworkCore;
namespace HopFrame.Core.Config;
@@ -7,6 +8,7 @@ public class HopFrameConfig {
public bool DisplayUserInfo { get; set; } = true;
public string? BasePolicy { get; set; }
public string? LoginPageRewrite { get; set; }
public List<HopEventHandler> Handlers { get; } = new();
}
/// <summary>
@@ -38,7 +40,7 @@ public class HopFrameConfigurator(HopFrameConfig config) {
/// <returns>The configurator used for the DbContext</returns>
/// <seealso cref="DbContextConfigurator{TDbContext}"/>
public DbContextConfigurator<TDbContext> AddDbContext<TDbContext>() where TDbContext : DbContext {
var context = new DbContextConfig(typeof(TDbContext));
var context = new DbContextConfig(typeof(TDbContext), InnerConfig);
InnerConfig.Contexts.Add(context);
return new DbContextConfigurator<TDbContext>(context);
}

View File

@@ -2,6 +2,7 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq.Expressions;
using System.Reflection;
using HopFrame.Core.Events;
namespace HopFrame.Core.Config;
@@ -188,6 +189,36 @@ public class TableConfigurator<TModel>(TableConfig config) {
InnerConfig.DeletePolicy = policy;
return this;
}
/// <summary>
/// Adds an event handler of the provided type
/// </summary>
/// <param name="type">The type of event that triggers the handler</param>
/// <param name="handler">The handler delegate</param>
public TableConfigurator<TModel> AddEventHandler(EventType type, Func<TModel, IServiceProvider, Task> handler) {
var eventName = EventTypes.ConstructEventName(type, InnerConfig);
var handlerStore = new HopEventHandler(eventName, (o, provider) => handler.Invoke((TModel)o, provider));
InnerConfig.ContextConfig.ParentConfig.Handlers.Add(handlerStore);
return this;
}
/// <summary>
/// Adds an event handler of the provided type
/// </summary>
/// <param name="type">The type of event that triggers the handler</param>
/// <param name="handler">The handler delegate</param>
public TableConfigurator<TModel> AddEventHandler(EventType type, Action<TModel, IServiceProvider> handler) {
var eventName = EventTypes.ConstructEventName(type, InnerConfig);
var handlerStore = new HopEventHandler(eventName, (o, provider) => {
handler.Invoke((TModel)o, provider);
return Task.CompletedTask;
});
InnerConfig.ContextConfig.ParentConfig.Handlers.Add(handlerStore);
return this;
}
internal static PropertyInfo GetPropertyInfo<TSource, TProperty>(Expression<Func<TSource, TProperty>> propertyLambda) {
if (propertyLambda.Body is not MemberExpression member) {