From 39641f18a837c35e8fab338e66178fa43851509b Mon Sep 17 00:00:00 2001 From: Leon Hoppe Date: Sat, 1 Feb 2025 11:50:52 +0100 Subject: [PATCH] Added modular event system --- .idea/.idea.HopFrame/.idea/workspace.xml | 70 +++++++++++++++++-- src/HopFrame.Core/Config/DbContextConfig.cs | 7 +- src/HopFrame.Core/Config/HopFrameConfig.cs | 6 +- src/HopFrame.Core/Config/TableConfig.cs | 31 ++++++++ src/HopFrame.Core/Events/EventTypes.cs | 31 ++++++++ src/HopFrame.Core/Events/HopEventHandler.cs | 7 ++ src/HopFrame.Core/Events/IEventEmitter.cs | 14 ++++ .../ServiceCollectionExtensions.cs | 4 +- .../Services/Implementations/EventEmitter.cs | 39 +++++++++++ .../Components/Pages/HopFrameTablePage.razor | 11 ++- .../ServiceCollectionExtensions.cs | 1 + testing/HopFrame.Testing/Program.cs | 1 + .../Config/DbContextConfiguratorTests.cs | 4 +- .../Config/PropertyConfiguratorTests.cs | 30 ++++---- .../Config/TableConfiguratorTests.cs | 28 ++++---- .../Services/ContextExplorerTests.cs | 20 +++--- .../Services/DisplayPropertyTests.cs | 4 +- .../Services/TableManagerTests.cs | 12 ++-- .../Components/Dialogs/HopFrameEditorTests.cs | 2 +- .../Layout/HopFrameSideMenuTests.cs | 2 +- .../Components/Pages/HopFrameHomeTests.cs | 2 +- .../Pages/HopFrameTablePageTests.cs | 4 +- 22 files changed, 262 insertions(+), 68 deletions(-) create mode 100644 src/HopFrame.Core/Events/EventTypes.cs create mode 100644 src/HopFrame.Core/Events/HopEventHandler.cs create mode 100644 src/HopFrame.Core/Events/IEventEmitter.cs create mode 100644 src/HopFrame.Core/Services/Implementations/EventEmitter.cs diff --git a/.idea/.idea.HopFrame/.idea/workspace.xml b/.idea/.idea.HopFrame/.idea/workspace.xml index 9070783..29bf02a 100644 --- a/.idea/.idea.HopFrame/.idea/workspace.xml +++ b/.idea/.idea.HopFrame/.idea/workspace.xml @@ -12,8 +12,28 @@ + + + + + + + + + + + + + + + + + + + + @@ -521,8 +558,6 @@ + + + + + file://$PROJECT_DIR$/src/HopFrame.Core/Services/Implementations/EventEmitter.cs + 19 + + + + + + + + + \ No newline at end of file diff --git a/src/HopFrame.Core/Config/DbContextConfig.cs b/src/HopFrame.Core/Config/DbContextConfig.cs index 9daed12..005e0bf 100644 --- a/src/HopFrame.Core/Config/DbContextConfig.cs +++ b/src/HopFrame.Core/Config/DbContextConfig.cs @@ -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 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; diff --git a/src/HopFrame.Core/Config/HopFrameConfig.cs b/src/HopFrame.Core/Config/HopFrameConfig.cs index 732d367..d5593bd 100644 --- a/src/HopFrame.Core/Config/HopFrameConfig.cs +++ b/src/HopFrame.Core/Config/HopFrameConfig.cs @@ -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 Handlers { get; } = new(); } /// @@ -38,7 +40,7 @@ public class HopFrameConfigurator(HopFrameConfig config) { /// The configurator used for the DbContext /// public DbContextConfigurator AddDbContext() where TDbContext : DbContext { - var context = new DbContextConfig(typeof(TDbContext)); + var context = new DbContextConfig(typeof(TDbContext), InnerConfig); InnerConfig.Contexts.Add(context); return new DbContextConfigurator(context); } diff --git a/src/HopFrame.Core/Config/TableConfig.cs b/src/HopFrame.Core/Config/TableConfig.cs index 97b40ce..fe5d9e3 100644 --- a/src/HopFrame.Core/Config/TableConfig.cs +++ b/src/HopFrame.Core/Config/TableConfig.cs @@ -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(TableConfig config) { InnerConfig.DeletePolicy = policy; return this; } + + /// + /// Adds an event handler of the provided type + /// + /// The type of event that triggers the handler + /// The handler delegate + public TableConfigurator AddEventHandler(EventType type, Func 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; + } + + /// + /// Adds an event handler of the provided type + /// + /// The type of event that triggers the handler + /// The handler delegate + public TableConfigurator AddEventHandler(EventType type, Action 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(Expression> propertyLambda) { if (propertyLambda.Body is not MemberExpression member) { diff --git a/src/HopFrame.Core/Events/EventTypes.cs b/src/HopFrame.Core/Events/EventTypes.cs new file mode 100644 index 0000000..d2ad751 --- /dev/null +++ b/src/HopFrame.Core/Events/EventTypes.cs @@ -0,0 +1,31 @@ +using HopFrame.Core.Config; + +namespace HopFrame.Core.Events; + +public static class EventTypes { + + private const string Prefix = "HopFrame."; + private const string CreateEntryPrefix = Prefix + "Entry.Create."; + private const string UpdateEntryPrefix = Prefix + "Entry.Update."; + private const string DeleteEntryPrefix = Prefix + "Entry.Delete."; + + public static string CreateEntry(TableConfig config) => CreateEntryPrefix + config.PropertyName; + public static string UpdateEntry(TableConfig config) => UpdateEntryPrefix + config.PropertyName; + public static string DeleteEntry(TableConfig config) => DeleteEntryPrefix + config.PropertyName; + + public static string ConstructEventName(EventType type, TableConfig config) { + return type switch { + EventType.CreateEntry => CreateEntry(config), + EventType.UpdateEntry => UpdateEntry(config), + EventType.DeleteEntry => DeleteEntry(config), + _ => Prefix + }; + } + +} + +public enum EventType { + CreateEntry = 0, + UpdateEntry = 1, + DeleteEntry = 2 +} diff --git a/src/HopFrame.Core/Events/HopEventHandler.cs b/src/HopFrame.Core/Events/HopEventHandler.cs new file mode 100644 index 0000000..b3767b4 --- /dev/null +++ b/src/HopFrame.Core/Events/HopEventHandler.cs @@ -0,0 +1,7 @@ +namespace HopFrame.Core.Events; + +public readonly struct HopEventHandler(string eventType, Func handler) { + public Guid Id { get; } = Guid.CreateVersion7(); + public Func Handler { get; } = handler; + public string EventType { get; } = eventType; +} diff --git a/src/HopFrame.Core/Events/IEventEmitter.cs b/src/HopFrame.Core/Events/IEventEmitter.cs new file mode 100644 index 0000000..804cf42 --- /dev/null +++ b/src/HopFrame.Core/Events/IEventEmitter.cs @@ -0,0 +1,14 @@ +namespace HopFrame.Core.Events; + +public interface IEventEmitter { + + Guid RegisterEventHandler(string @event, Func handler); + + bool RemoveEventHandler(Guid id); + + Task DispatchEvent(string @event, object argument = null!); + + void RemoveAllEventHandlers(string @event); + void RemoveAllEventHandlers(); + +} \ No newline at end of file diff --git a/src/HopFrame.Core/ServiceCollectionExtensions.cs b/src/HopFrame.Core/ServiceCollectionExtensions.cs index c8ea4df..804b6be 100644 --- a/src/HopFrame.Core/ServiceCollectionExtensions.cs +++ b/src/HopFrame.Core/ServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ -using HopFrame.Core.Services; +using HopFrame.Core.Events; +using HopFrame.Core.Services; using HopFrame.Core.Services.Implementations; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -15,6 +16,7 @@ public static class ServiceCollectionExtensions { public static IServiceCollection AddHopFrameServices(this IServiceCollection services) { services.AddScoped(); services.TryAddScoped(); + services.TryAddSingleton(); return services; } diff --git a/src/HopFrame.Core/Services/Implementations/EventEmitter.cs b/src/HopFrame.Core/Services/Implementations/EventEmitter.cs new file mode 100644 index 0000000..375e696 --- /dev/null +++ b/src/HopFrame.Core/Services/Implementations/EventEmitter.cs @@ -0,0 +1,39 @@ +using HopFrame.Core.Config; +using HopFrame.Core.Events; + +namespace HopFrame.Core.Services.Implementations; + +internal sealed class EventEmitter(IServiceProvider provider, HopFrameConfig config) : IEventEmitter { + + public Guid RegisterEventHandler(string @event, Func handler) { + var handlerStore = new HopEventHandler(@event, handler); + config.Handlers.Add(handlerStore); + return handlerStore.Id; + } + + public bool RemoveEventHandler(Guid id) { + var count = config.Handlers.RemoveAll(handler => handler.Id == id); + return count > 0; + } + + public async Task DispatchEvent(string @event, object argument = null!) { + var handlers = config.Handlers.Where(handler => handler.EventType == @event); + var tasks = new List(); + + foreach (var handler in handlers) { + var task = handler.Handler.Invoke(argument, provider); + tasks.Add(task); + } + + await Task.WhenAll(tasks); + } + + public void RemoveAllEventHandlers(string @event) { + config.Handlers.RemoveAll(handler => handler.EventType == @event); + } + + public void RemoveAllEventHandlers() { + config.Handlers.Clear(); + } + +} \ No newline at end of file diff --git a/src/HopFrame.Web/Components/Pages/HopFrameTablePage.razor b/src/HopFrame.Web/Components/Pages/HopFrameTablePage.razor index 75478ff..90884af 100644 --- a/src/HopFrame.Web/Components/Pages/HopFrameTablePage.razor +++ b/src/HopFrame.Web/Components/Pages/HopFrameTablePage.razor @@ -4,6 +4,7 @@ @implements IDisposable @using HopFrame.Core.Config +@using HopFrame.Core.Events @using HopFrame.Core.Services @using HopFrame.Web.Models @using Microsoft.JSInterop @@ -117,6 +118,7 @@ @inject IJSRuntime Js @inject IDialogService Dialogs @inject IHopFrameAuthHandler Handler +@inject IEventEmitter Emitter @code { @@ -229,6 +231,7 @@ if (result.Cancelled) return; await _manager!.DeleteItem(element); + await Emitter.DispatchEvent(EventTypes.DeleteEntry(_config!), element); await Reload(); } @@ -246,10 +249,14 @@ if (result.Cancelled) return; - if (element is null) + if (element is null) { await _manager!.AddItem(data!.CurrentObject!); - else + await Emitter.DispatchEvent(EventTypes.CreateEntry(_config!), data.CurrentObject!); + } + else { await _manager!.EditItem(data!.CurrentObject!); + await Emitter.DispatchEvent(EventTypes.UpdateEntry(_config!), data.CurrentObject!); + } await Reload(); } diff --git a/src/HopFrame.Web/ServiceCollectionExtensions.cs b/src/HopFrame.Web/ServiceCollectionExtensions.cs index 775766c..6e72caf 100644 --- a/src/HopFrame.Web/ServiceCollectionExtensions.cs +++ b/src/HopFrame.Web/ServiceCollectionExtensions.cs @@ -1,5 +1,6 @@ using HopFrame.Core; using HopFrame.Core.Config; +using HopFrame.Core.Events; using HopFrame.Web.Components; using HopFrame.Web.Components.Pages; using Microsoft.Extensions.DependencyInjection; diff --git a/testing/HopFrame.Testing/Program.cs b/testing/HopFrame.Testing/Program.cs index e18f72f..97d1ad8 100644 --- a/testing/HopFrame.Testing/Program.cs +++ b/testing/HopFrame.Testing/Program.cs @@ -1,4 +1,5 @@ using System.Collections; +using HopFrame.Core.Events; using HopFrame.Testing; using Microsoft.FluentUI.AspNetCore.Components; using HopFrame.Testing.Components; diff --git a/tests/HopFrame.Tests.Core/Config/DbContextConfiguratorTests.cs b/tests/HopFrame.Tests.Core/Config/DbContextConfiguratorTests.cs index ce0fd1c..03f7df6 100644 --- a/tests/HopFrame.Tests.Core/Config/DbContextConfiguratorTests.cs +++ b/tests/HopFrame.Tests.Core/Config/DbContextConfiguratorTests.cs @@ -8,7 +8,7 @@ public class DbContextConfiguratorTests { [Fact] public void Table_WithConfigurator_InvokesConfigurator() { // Arrange - var dbContextConfig = new DbContextConfig(typeof(MockDbContext)); + var dbContextConfig = new DbContextConfig(typeof(MockDbContext), null!); var configurator = new DbContextConfigurator(dbContextConfig); var mockConfigurator = new Mock>>(); @@ -22,7 +22,7 @@ public class DbContextConfiguratorTests { [Fact] public void Table_ReturnsCorrectTableConfigurator() { // Arrange - var dbContextConfig = new DbContextConfig(typeof(MockDbContext)); + var dbContextConfig = new DbContextConfig(typeof(MockDbContext), null!); var configurator = new DbContextConfigurator(dbContextConfig); // Act diff --git a/tests/HopFrame.Tests.Core/Config/PropertyConfiguratorTests.cs b/tests/HopFrame.Tests.Core/Config/PropertyConfiguratorTests.cs index 312d77a..bb9f2a6 100644 --- a/tests/HopFrame.Tests.Core/Config/PropertyConfiguratorTests.cs +++ b/tests/HopFrame.Tests.Core/Config/PropertyConfiguratorTests.cs @@ -9,7 +9,7 @@ public class PropertyConfiguratorTests { public void SetDisplayName_SetsNameProperty() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); var displayName = "ID"; @@ -24,7 +24,7 @@ public class PropertyConfiguratorTests { public void List_SetsListAndSearchableProperties() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); // Act @@ -39,7 +39,7 @@ public class PropertyConfiguratorTests { public void IsSortable_SetsSortableProperty() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); // Act @@ -53,7 +53,7 @@ public class PropertyConfiguratorTests { public void IsSearchable_SetsSearchableProperty() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); // Act @@ -67,7 +67,7 @@ public class PropertyConfiguratorTests { public void SetDisplayedProperty_SetsDisplayedProperty() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); Expression> propertyExpression = model => model.Id; @@ -83,7 +83,7 @@ public class PropertyConfiguratorTests { public void Format_SetsFormatter() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); Func formatter = (val, _) => val.ToString(); @@ -98,7 +98,7 @@ public class PropertyConfiguratorTests { public void SetParser_SetsParser() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); Func parser = (str, _) => int.Parse(str); @@ -113,7 +113,7 @@ public class PropertyConfiguratorTests { public void SetEditable_SetsEditableProperty() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); // Act @@ -127,7 +127,7 @@ public class PropertyConfiguratorTests { public void SetCreatable_SetsCreatableProperty() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); // Act @@ -141,7 +141,7 @@ public class PropertyConfiguratorTests { public void DisplayValue_SetsDisplayValueProperty() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); // Act @@ -155,7 +155,7 @@ public class PropertyConfiguratorTests { public void IsTextArea_SetsTextAreaProperty() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); // Act @@ -169,7 +169,7 @@ public class PropertyConfiguratorTests { public void SetTextAreaRows_SetsTextAreaRowsProperty() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); var rows = 10; @@ -184,7 +184,7 @@ public class PropertyConfiguratorTests { public void SetValidator_SetsValidator() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); Func> validator = (_, _) => new List(); @@ -199,7 +199,7 @@ public class PropertyConfiguratorTests { public void SetOrderIndex_SetsOrderProperty() { // Arrange var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0), 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0), 0); var configurator = new PropertyConfigurator(propertyConfig); var orderIndex = 1; @@ -213,7 +213,7 @@ public class PropertyConfiguratorTests { [Fact] public void Constructor_SetsTableProperty() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); // Act var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, tableConfig, 0); diff --git a/tests/HopFrame.Tests.Core/Config/TableConfiguratorTests.cs b/tests/HopFrame.Tests.Core/Config/TableConfiguratorTests.cs index 10bbc3d..3a9f655 100644 --- a/tests/HopFrame.Tests.Core/Config/TableConfiguratorTests.cs +++ b/tests/HopFrame.Tests.Core/Config/TableConfiguratorTests.cs @@ -9,7 +9,7 @@ public class TableConfiguratorTests { public void Ignore_SetsIgnoredProperty() { // Arrange var tableConfig = - new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); // Act @@ -22,7 +22,7 @@ public class TableConfiguratorTests { [Fact] public void Property_ReturnsCorrectPropertyConfigurator() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); Expression> propertyExpression = model => model.Id; @@ -35,7 +35,7 @@ public class TableConfiguratorTests { public void Property_WithConfigurator_ReturnsCorrectPropertyConfigurator() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); Expression> propertyExpression = model => model.Id; @@ -52,7 +52,7 @@ public class TableConfiguratorTests { [Fact] public void AddVirtualProperty_AddsVirtualPropertyToConfig() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); Func template = (model, _) => model.Name!; @@ -70,7 +70,7 @@ public class TableConfiguratorTests { [Fact] public void AddVirtualProperty_WithConfigurator_AddsVirtualPropertyToConfig() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); Func template = (model, _) => model.Name!; @@ -91,7 +91,7 @@ public class TableConfiguratorTests { [Fact] public void SetDisplayName_SetsDisplayNameProperty() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); var displayName = "Mock Model Display Name"; @@ -105,7 +105,7 @@ public class TableConfiguratorTests { [Fact] public void SetDescription_SetsDescriptionProperty() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); var description = "Mock Model Description"; @@ -119,7 +119,7 @@ public class TableConfiguratorTests { [Fact] public void SetOrderIndex_SetsOrderIndexProperty() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); var orderIndex = 1; @@ -133,7 +133,7 @@ public class TableConfiguratorTests { [Fact] public void SetViewPolicy_SetsViewPolicyProperty() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); var policy = "ViewPolicy"; @@ -147,7 +147,7 @@ public class TableConfiguratorTests { [Fact] public void SetUpdatePolicy_SetsUpdatePolicyProperty() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); var policy = "UpdatePolicy"; @@ -161,7 +161,7 @@ public class TableConfiguratorTests { [Fact] public void SetCreatePolicy_SetsCreatePolicyProperty() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); var policy = "CreatePolicy"; @@ -175,7 +175,7 @@ public class TableConfiguratorTests { [Fact] public void SetDeletePolicy_SetsDeletePolicyProperty() { // Arrange - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "MockModels", 0); var configurator = new TableConfigurator(tableConfig); var policy = "DeletePolicy"; @@ -189,7 +189,7 @@ public class TableConfiguratorTests { [Fact] public void Constructor_WithKeyProperty_DisablesEdit() { // Act - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel2), "Models2", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel2), "Models2", 0); var prop = tableConfig.Properties.SingleOrDefault(prop => prop.Info.Name == nameof(MockModel2.Id)); // Assert @@ -200,7 +200,7 @@ public class TableConfiguratorTests { [Fact] public void Constructor_WithGeneratedProperty_DisablesEditAndCreate() { // Act - var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel2), "Models2", 0); + var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel2), "Models2", 0); var prop = tableConfig.Properties.SingleOrDefault(prop => prop.Info.Name == nameof(MockModel2.Number)); // Assert diff --git a/tests/HopFrame.Tests.Core/Services/ContextExplorerTests.cs b/tests/HopFrame.Tests.Core/Services/ContextExplorerTests.cs index c0b54a1..d092f88 100644 --- a/tests/HopFrame.Tests.Core/Services/ContextExplorerTests.cs +++ b/tests/HopFrame.Tests.Core/Services/ContextExplorerTests.cs @@ -11,7 +11,7 @@ public class ContextExplorerTests { public void GetTables_ReturnsNonIgnoredTables() { // Arrange var config = new HopFrameConfig(); - var contextConfig = new DbContextConfig(typeof(MockDbContext)); + var contextConfig = new DbContextConfig(typeof(MockDbContext), null!); var tableConfig1 = contextConfig.Tables[0]; var tableConfig2 = contextConfig.Tables[1]; config.Contexts.Add(contextConfig); @@ -33,7 +33,7 @@ public class ContextExplorerTests { public void GetTable_ByDisplayName_ReturnsCorrectTable() { // Arrange var config = new HopFrameConfig(); - var contextConfig = new DbContextConfig(typeof(MockDbContext)); + var contextConfig = new DbContextConfig(typeof(MockDbContext), null!); var tableConfig = contextConfig.Tables[0]; config.Contexts.Add(contextConfig); tableConfig.DisplayName = "TestTable"; @@ -54,7 +54,7 @@ public class ContextExplorerTests { public void GetTable_ByDisplayName_ReturnsNullIfTableNotFound() { // Arrange var config = new HopFrameConfig(); - var contextConfig = new DbContextConfig(typeof(MockDbContext)); + var contextConfig = new DbContextConfig(typeof(MockDbContext), null!); var tableConfig = contextConfig.Tables[0]; config.Contexts.Add(contextConfig); tableConfig.DisplayName = "TestTable"; @@ -74,7 +74,7 @@ public class ContextExplorerTests { public void GetTable_ByType_ReturnsCorrectTable() { // Arrange var config = new HopFrameConfig(); - var contextConfig = new DbContextConfig(typeof(MockDbContext)); + var contextConfig = new DbContextConfig(typeof(MockDbContext), null!); var tableConfig = contextConfig.Tables[0]; config.Contexts.Add(contextConfig); @@ -94,7 +94,7 @@ public class ContextExplorerTests { public void GetTable_ByType_ReturnsNullIfTableNotFound() { // Arrange var config = new HopFrameConfig(); - var contextConfig = new DbContextConfig(typeof(MockDbContext)); + var contextConfig = new DbContextConfig(typeof(MockDbContext), null!); var tableConfig = contextConfig.Tables[0]; config.Contexts.Add(contextConfig); @@ -113,7 +113,7 @@ public class ContextExplorerTests { public void GetTableManager_ReturnsCorrectTableManager() { // Arrange var config = new HopFrameConfig(); - var contextConfig = new DbContextConfig(typeof(MockDbContext)); + var contextConfig = new DbContextConfig(typeof(MockDbContext), null!); var tableConfig = new TableConfig(contextConfig, typeof(MockModel), "Models", 0); contextConfig.Tables.Add(tableConfig); config.Contexts.Add(contextConfig); @@ -135,7 +135,7 @@ public class ContextExplorerTests { public void GetTableManager_ReturnsNullIfDbContextNotFound() { // Arrange var config = new HopFrameConfig(); - var contextConfig = new DbContextConfig(typeof(MockDbContext)); + var contextConfig = new DbContextConfig(typeof(MockDbContext), null!); var tableConfig = new TableConfig(contextConfig, typeof(MockModel), "MockModels", 0); contextConfig.Tables.Add(tableConfig); config.Contexts.Add(contextConfig); @@ -154,7 +154,7 @@ public class ContextExplorerTests { public void GetTableManager_ReturnsNullIfTableNotFound() { // Arrange var config = new HopFrameConfig(); - var contextConfig = new DbContextConfig(typeof(MockDbContext)); + var contextConfig = new DbContextConfig(typeof(MockDbContext), null!); var tableConfig = new TableConfig(contextConfig, typeof(MockModel), "Models", 0); contextConfig.Tables.Add(tableConfig); config.Contexts.Add(contextConfig); @@ -175,7 +175,7 @@ public class ContextExplorerTests { public void SeedTableData_SetsTableSeededFlag() { // Arrange var config = new HopFrameConfig(); - var contextConfig = new DbContextConfig(typeof(MockDbContext)); + var contextConfig = new DbContextConfig(typeof(MockDbContext), null!); var tableConfig = contextConfig.Tables[0]; config.Contexts.Add(contextConfig); @@ -194,7 +194,7 @@ public class ContextExplorerTests { public void SeedTableData_SetsTablePropertiesCorrectly() { // Arrange var config = new HopFrameConfig(); - var contextConfig = new DbContextConfig(typeof(MockDbContext)); + var contextConfig = new DbContextConfig(typeof(MockDbContext), null!); var tableConfig = contextConfig.Tables[0]; var tableConfig2 = contextConfig.Tables[1]; config.Contexts.Add(contextConfig); diff --git a/tests/HopFrame.Tests.Core/Services/DisplayPropertyTests.cs b/tests/HopFrame.Tests.Core/Services/DisplayPropertyTests.cs index 6464b3f..922e7db 100644 --- a/tests/HopFrame.Tests.Core/Services/DisplayPropertyTests.cs +++ b/tests/HopFrame.Tests.Core/Services/DisplayPropertyTests.cs @@ -17,7 +17,7 @@ public class DisplayPropertyTests { var contextMock = new Mock(); _providerMock = new Mock(); _explorerMock = new Mock(); - _config = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "Models", 0); + _config = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "Models", 0); _tableManager = new TableManager(contextMock.Object, _config, _explorerMock.Object, _providerMock.Object); } @@ -106,7 +106,7 @@ public class DisplayPropertyTests { _explorerMock .Setup(e => e.GetTable(item.Inner.GetType())) - .Returns(new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "Models", 0) { + .Returns(new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "Models", 0) { Properties = { innerPropConfig } }); diff --git a/tests/HopFrame.Tests.Core/Services/TableManagerTests.cs b/tests/HopFrame.Tests.Core/Services/TableManagerTests.cs index 01772f2..dc39c8e 100644 --- a/tests/HopFrame.Tests.Core/Services/TableManagerTests.cs +++ b/tests/HopFrame.Tests.Core/Services/TableManagerTests.cs @@ -48,7 +48,7 @@ public class TableManagerTests { new MockModel { Id = 3, Name = "Item3" } }; var dbContext = CreateMockDbContext(data); - var config = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "Models", 0); + var config = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "Models", 0); var explorer = new Mock(); var provider = new Mock(); var manager = new TableManager(dbContext.Object, config, explorer.Object, provider.Object); @@ -70,7 +70,7 @@ public class TableManagerTests { new MockModel { Id = 3, Name = "TestItem" } }; var dbContext = CreateMockDbContext(data); - var config = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "Models", 0); + var config = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "Models", 0); config.Properties.Add(new PropertyConfig(typeof(MockModel).GetProperty("Name")!, config, 0) { Searchable = true }); var explorer = new Mock(); @@ -96,7 +96,7 @@ public class TableManagerTests { new MockModel { Id = 3, Name = "Item3" } }; var dbContext = new MockDbContext(); - var config = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "Models", 0); + var config = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "Models", 0); var explorer = new Mock(); var provider = new Mock(); var manager = new TableManager(dbContext, config, explorer.Object, provider.Object); @@ -118,7 +118,7 @@ public class TableManagerTests { new MockModel { Id = 2, Name = "Item2" } }; var dbContext = CreateMockDbContext(data); - var config = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "Models", 0); + var config = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "Models", 0); var explorer = new Mock(); var provider = new Mock(); var manager = new TableManager(dbContext.Object, config, explorer.Object, provider.Object); @@ -139,7 +139,7 @@ public class TableManagerTests { new MockModel { Id = 1, Name = "Item1" } }; var dbContext = CreateMockDbContext(data); - var config = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "Models", 0); + var config = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "Models", 0); var explorer = new Mock(); var provider = new Mock(); var manager = new TableManager(dbContext.Object, config, explorer.Object, provider.Object); @@ -156,7 +156,7 @@ public class TableManagerTests { // Arrange var data = new List(); var dbContext = CreateMockDbContext(data); - var config = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "Models", 0); + var config = new TableConfig(new DbContextConfig(typeof(MockDbContext), null!), typeof(MockModel), "Models", 0); var explorer = new Mock(); var provider = new Mock(); var manager = new TableManager(dbContext.Object, config, explorer.Object, provider.Object); diff --git a/tests/HopFrame.Tests.Web/Components/Dialogs/HopFrameEditorTests.cs b/tests/HopFrame.Tests.Web/Components/Dialogs/HopFrameEditorTests.cs index d32bcf8..7904528 100644 --- a/tests/HopFrame.Tests.Web/Components/Dialogs/HopFrameEditorTests.cs +++ b/tests/HopFrame.Tests.Web/Components/Dialogs/HopFrameEditorTests.cs @@ -22,7 +22,7 @@ public class HopFrameEditorTests : TestContext { var dialogServiceMock = new Mock(); var toastServiceMock = new Mock(); var serviceProviderMock = new Mock(); - var contextConfig = new DbContextConfig(typeof(MyDbContext)); + var contextConfig = new DbContextConfig(typeof(MyDbContext), null!); var tableConfig = new TableConfig(contextConfig, typeof(MyTable), "Table1", 0) { DisplayName = "Table1", ViewPolicy = "Policy1" diff --git a/tests/HopFrame.Tests.Web/Components/Layout/HopFrameSideMenuTests.cs b/tests/HopFrame.Tests.Web/Components/Layout/HopFrameSideMenuTests.cs index 75943e4..32757a9 100644 --- a/tests/HopFrame.Tests.Web/Components/Layout/HopFrameSideMenuTests.cs +++ b/tests/HopFrame.Tests.Web/Components/Layout/HopFrameSideMenuTests.cs @@ -17,7 +17,7 @@ public class HopFrameSideMenuTests : TestContext { // Arrange var contextExplorerMock = new Mock(); var authHandlerMock = new Mock(); - var contextConfig = new DbContextConfig(typeof(MyDbContext)); + var contextConfig = new DbContextConfig(typeof(MyDbContext), null!); var tableConfigs = new List { new (contextConfig, typeof(MyTable), "Table1", 0), new (contextConfig, typeof(MyTable2), "Table2", 1) diff --git a/tests/HopFrame.Tests.Web/Components/Pages/HopFrameHomeTests.cs b/tests/HopFrame.Tests.Web/Components/Pages/HopFrameHomeTests.cs index b700920..ab44bc8 100644 --- a/tests/HopFrame.Tests.Web/Components/Pages/HopFrameHomeTests.cs +++ b/tests/HopFrame.Tests.Web/Components/Pages/HopFrameHomeTests.cs @@ -16,7 +16,7 @@ public class HopFrameHomeTests : TestContext { // Arrange var contextExplorerMock = new Mock(); var authHandlerMock = new Mock(); - var contextConfig = new DbContextConfig(typeof(MyDbContext)); + var contextConfig = new DbContextConfig(typeof(MyDbContext), null!); var tableConfigs = new List { new TableConfig(contextConfig, typeof(MyTable), "Table1", 0) { DisplayName = "Table1", diff --git a/tests/HopFrame.Tests.Web/Components/Pages/HopFrameTablePageTests.cs b/tests/HopFrame.Tests.Web/Components/Pages/HopFrameTablePageTests.cs index 7b8a09f..1b12349 100644 --- a/tests/HopFrame.Tests.Web/Components/Pages/HopFrameTablePageTests.cs +++ b/tests/HopFrame.Tests.Web/Components/Pages/HopFrameTablePageTests.cs @@ -19,7 +19,7 @@ public class HopFrameTablePageTests : TestContext { var contextExplorerMock = new Mock(); var authHandlerMock = new Mock(); var dialogServiceMock = new Mock(); - var contextConfig = new DbContextConfig(typeof(MyDbContext)); + var contextConfig = new DbContextConfig(typeof(MyDbContext), null!); var managerMock = new Mock(); var tableConfig = new TableConfig(contextConfig, typeof(MyTable), "Table1", 0) { DisplayName = "Table1", @@ -63,7 +63,7 @@ public class HopFrameTablePageTests : TestContext { var contextExplorerMock = new Mock(); var authHandlerMock = new Mock(); var dialogServiceMock = new Mock(); - var contextConfig = new DbContextConfig(typeof(MyDbContext)); + var contextConfig = new DbContextConfig(typeof(MyDbContext), null!); var tableConfig = new TableConfig(contextConfig, typeof(MyTable), "Table1", 0) { DisplayName = "Table1", ViewPolicy = "Policy1"