Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modularity Fix (backport #3219) #3227

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
{
}
}
}