Skip to content

Commit

Permalink
Merge pull request #3219 from PrismLibrary/dev/ds/module-fix
Browse files Browse the repository at this point in the history
Modularity Fix
  • Loading branch information
dansiegel authored Aug 14, 2024
2 parents e275b7e + ec5e8a6 commit 6a86364
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 10 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("Modules 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
14 changes: 5 additions & 9 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,15 +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 =>
{
var services = builder.MauiBuilder.Services;
services.AddSingleton<IModuleCatalog, ModuleCatalog>();
services.AddSingleton<IModuleManager, ModuleManager>();
services.AddSingleton<IModuleInitializer, ModuleInitializer>();
}
container.TryRegisterSingleton<IModuleCatalog, ModuleCatalog>();
container.TryRegisterSingleton<IModuleManager, ModuleManager>();
container.TryRegisterSingleton<IModuleInitializer, ModuleInitializer>();
});

s_didRegisterModules = true;
return builder.OnInitialized(container =>
{
var moduleCatalog = container.Resolve<IModuleCatalog>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
namespace Prism.DryIoc.Maui.Tests.Fixtures.Modularity;

public class ModuleCatalogTests(ITestOutputHelper testOutputHelper) : TestBase(testOutputHelper)
{
public readonly IModuleInfo ModuleA = new ModuleInfo(typeof(MockModuleA));
public readonly IModuleInfo ModuleB = new ModuleInfo(typeof(MockModuleB));

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

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

var moduleCatalog = app.Services.GetService<IModuleCatalog>();
Assert.Equal(2, moduleCatalog.Modules.Count());
Assert.Contains(ModuleA.ModuleType, moduleCatalog.Modules.Select(m => m.ModuleType));
Assert.Contains(ModuleB.ModuleType, moduleCatalog.Modules.Select(m => m.ModuleType));
}

public class CustomMoudleCatalog : ModuleCatalogBase { }

public class MockModuleA : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
}

public void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}

public class MockModuleB : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
}

public void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
}

0 comments on commit 6a86364

Please sign in to comment.