Skip to content

Commit

Permalink
feat: allow injecting Modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Aug 5, 2024
1 parent 01a4940 commit 3100f41
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Maui/Prism.Maui/Modularity/ModuleCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Prism.Modularity;
#else
[ContentProperty(nameof(Items))]
#endif
public class ModuleCatalog : ModuleCatalogBase
public class ModuleCatalog(IEnumerable<IModuleInfo> modules) : ModuleCatalogBase(modules)
{

}
6 changes: 6 additions & 0 deletions src/Maui/Prism.Maui/PrismAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,21 @@ internal void OnInitialized()
{
try
{
logger.LogDebug("Initializing modules.");
var manager = _container.Resolve<IModuleManager>();
manager.Run();
logger.LogDebug("Moudles Initialized.");
}
catch (Exception ex)
{
logger.LogError(ex, "An error ocurred while initializing the Modules.");
throw new PrismInitializationException("An error occurred while initializing the Modules.", ex);
}
}
else
{
logger.LogDebug("No Modules found to initialize.");
}

var navRegistry = _container.Resolve<INavigationRegistry>();
if (!navRegistry.IsRegistered(nameof(NavigationPage)))
Expand Down
18 changes: 6 additions & 12 deletions src/Maui/Prism.Maui/PrismAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace Prism;
/// </summary>
public static class PrismAppBuilderExtensions
{
private static bool s_didRegisterModules = false;

/// <summary>
/// Configures the <see cref="MauiAppBuilder"/> to use Prism with a callback for the <see cref="PrismAppBuilder"/>
/// </summary>
Expand Down Expand Up @@ -45,17 +43,13 @@ public static PrismAppBuilder OnInitialized(this PrismAppBuilder builder, Action
/// <param name="configureCatalog">Delegate to configure the <see cref="IModuleCatalog"/>.</param>
public static PrismAppBuilder ConfigureModuleCatalog(this PrismAppBuilder builder, Action<IModuleCatalog> configureCatalog)
{
if (!s_didRegisterModules)
builder.RegisterTypes(container =>
{
builder.RegisterTypes(container =>
{
container.TryRegisterSingleton<IModuleCatalog, ModuleCatalog>();
container.TryRegisterSingleton<IModuleManager, ModuleManager>();
container.TryRegisterSingleton<IModuleInitializer, ModuleInitializer>();
});
}

s_didRegisterModules = true;
container.TryRegisterSingleton<IModuleCatalog, ModuleCatalog>();
container.TryRegisterSingleton<IModuleManager, ModuleManager>();
container.TryRegisterSingleton<IModuleInitializer, ModuleInitializer>();
});

return builder.OnInitialized(container =>
{
var moduleCatalog = container.Resolve<IModuleCatalog>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IModuleCatalog, CustomMoudleCatalog>())
.ConfigureModuleCatalog(catalog => { });
});
var app = builder.Build();

var moduleCatalog = app.Services.GetService<IModuleCatalog>();
Assert.NotNull(moduleCatalog);
Assert.IsType<CustomMoudleCatalog>(moduleCatalog);
}

[Fact]
public void InjectsModulesFromDI()
{
var builder = CreateBuilder(prism =>
{
prism.RegisterTypes(c => c.RegisterInstance<IModuleInfo>(ModuleA))
.ConfigureModuleCatalog(c => { });
});
var app = builder.Build();

var moduleCatalog = app.Services.GetService<IModuleCatalog>();
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<MockModuleA>()));
var app = builder.Build();

var moduleCatalog = app.Services.GetService<IModuleCatalog>();
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)
{
}
}
}

0 comments on commit 3100f41

Please sign in to comment.