From c7bf3f8e87f3854d158a9f9461231e659cf63c56 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Mon, 5 Aug 2024 06:46:32 -0600 Subject: [PATCH] feat: allow injecting Modules --- .../Prism.Maui/Modularity/ModuleCatalog.cs | 2 +- src/Maui/Prism.Maui/PrismAppBuilder.cs | 6 ++ .../Prism.Maui/PrismAppBuilderExtensions.cs | 18 ++---- .../Fixtures/Modularity/ModuleCatalogTests.cs | 61 +++++++++++++++++++ 4 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 tests/Maui/Prism.DryIoc.Maui.Tests/Fixtures/Modularity/ModuleCatalogTests.cs diff --git a/src/Maui/Prism.Maui/Modularity/ModuleCatalog.cs b/src/Maui/Prism.Maui/Modularity/ModuleCatalog.cs index 943acea55..2e3ec99ac 100644 --- a/src/Maui/Prism.Maui/Modularity/ModuleCatalog.cs +++ b/src/Maui/Prism.Maui/Modularity/ModuleCatalog.cs @@ -10,7 +10,7 @@ namespace Prism.Modularity; #else [ContentProperty(nameof(Items))] #endif -public class ModuleCatalog : ModuleCatalogBase +public class ModuleCatalog(IEnumerable modules) : ModuleCatalogBase(modules) { } diff --git a/src/Maui/Prism.Maui/PrismAppBuilder.cs b/src/Maui/Prism.Maui/PrismAppBuilder.cs index 71cb61933..2d77e3a1a 100644 --- a/src/Maui/Prism.Maui/PrismAppBuilder.cs +++ b/src/Maui/Prism.Maui/PrismAppBuilder.cs @@ -205,8 +205,10 @@ internal void OnInitialized() { try { + logger.LogDebug("Initializing modules."); var manager = _container.Resolve(); manager.Run(); + logger.LogDebug("Modules Initialized."); } catch (Exception ex) { @@ -214,6 +216,10 @@ internal void OnInitialized() throw new PrismInitializationException("An error occurred while initializing the Modules.", ex); } } + else + { + logger.LogDebug("No Modules found to initialize."); + } var navRegistry = _container.Resolve(); if (!navRegistry.IsRegistered(nameof(NavigationPage))) diff --git a/src/Maui/Prism.Maui/PrismAppBuilderExtensions.cs b/src/Maui/Prism.Maui/PrismAppBuilderExtensions.cs index 0415ae580..db037065a 100644 --- a/src/Maui/Prism.Maui/PrismAppBuilderExtensions.cs +++ b/src/Maui/Prism.Maui/PrismAppBuilderExtensions.cs @@ -11,8 +11,6 @@ namespace Prism; /// public static class PrismAppBuilderExtensions { - private static bool s_didRegisterModules = false; - /// /// Configures the to use Prism with a callback for the /// @@ -45,17 +43,13 @@ public static PrismAppBuilder OnInitialized(this PrismAppBuilder builder, Action /// Delegate to configure the . public static PrismAppBuilder ConfigureModuleCatalog(this PrismAppBuilder builder, Action configureCatalog) { - if (!s_didRegisterModules) + builder.RegisterTypes(container => { - builder.RegisterTypes(container => - { - container.TryRegisterSingleton(); - container.TryRegisterSingleton(); - container.TryRegisterSingleton(); - }); - } - - s_didRegisterModules = true; + container.TryRegisterSingleton(); + container.TryRegisterSingleton(); + container.TryRegisterSingleton(); + }); + return builder.OnInitialized(container => { var moduleCatalog = container.Resolve(); diff --git a/tests/Maui/Prism.DryIoc.Maui.Tests/Fixtures/Modularity/ModuleCatalogTests.cs b/tests/Maui/Prism.DryIoc.Maui.Tests/Fixtures/Modularity/ModuleCatalogTests.cs new file mode 100644 index 000000000..77ce0d203 --- /dev/null +++ b/tests/Maui/Prism.DryIoc.Maui.Tests/Fixtures/Modularity/ModuleCatalogTests.cs @@ -0,0 +1,61 @@ +namespace Prism.DryIoc.Maui.Tests.Fixtures.Modularity; + +public class ModuleCatalogTests(ITestOutputHelper testOutputHelper) : TestBase(testOutputHelper) +{ + public readonly IModuleInfo ModuleA = new ModuleInfo(typeof(MockModuleA)); + + [Fact] + public void UsesCustomModuleCatalog() + { + var builder = CreateBuilder(prism => + { + prism.RegisterTypes(c => c.RegisterSingleton()) + .ConfigureModuleCatalog(catalog => { }); + }); + var app = builder.Build(); + + var moduleCatalog = app.Services.GetService(); + Assert.NotNull(moduleCatalog); + Assert.IsType(moduleCatalog); + } + + [Fact] + public void InjectsModulesFromDI() + { + var builder = CreateBuilder(prism => + { + prism.RegisterTypes(c => c.RegisterInstance(ModuleA)) + .ConfigureModuleCatalog(c => { }); + }); + var app = builder.Build(); + + var moduleCatalog = app.Services.GetService(); + Assert.Single(moduleCatalog.Modules); + Assert.Equal(ModuleA.ModuleType, moduleCatalog.Modules.Single().ModuleType); + } + + [Fact] + public void InjectsModulesFromConfigureDelegate() + { + var builder = CreateBuilder(prism => + prism.ConfigureModuleCatalog(c => c.AddModule())); + var app = builder.Build(); + + var moduleCatalog = app.Services.GetService(); + Assert.Single(moduleCatalog.Modules); + Assert.Equal(ModuleA.ModuleType, moduleCatalog.Modules.Single().ModuleType); + } + + public class CustomMoudleCatalog : ModuleCatalogBase { } + + public class MockModuleA : IModule + { + public void OnInitialized(IContainerProvider containerProvider) + { + } + + public void RegisterTypes(IContainerRegistry containerRegistry) + { + } + } +}