2021-12-12 15:41:51 +01:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2021-11-30 14:32:10 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
|
|
|
|
namespace zero.Architecture;
|
|
|
|
|
|
2021-12-12 15:41:51 +01:00
|
|
|
public class ZeroModuleCollection : ZeroModule
|
2021-11-30 14:32:10 +01:00
|
|
|
{
|
2021-12-12 15:41:51 +01:00
|
|
|
ConcurrentDictionary<Type, IZeroModule> _modules = new();
|
2021-11-30 14:32:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a zero module
|
|
|
|
|
/// </summary>
|
2021-12-12 15:41:51 +01:00
|
|
|
public void Add<T>() where T : class, IZeroModule, new()
|
2021-11-30 14:32:10 +01:00
|
|
|
{
|
2021-12-12 15:41:51 +01:00
|
|
|
Add(new T());
|
2021-11-30 14:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a zero module
|
|
|
|
|
/// </summary>
|
2021-12-12 15:41:51 +01:00
|
|
|
public void Add<T>(T moduleInstance) where T : IZeroModule
|
2021-11-30 14:32:10 +01:00
|
|
|
{
|
2021-12-12 15:41:51 +01:00
|
|
|
Add(typeof(T), moduleInstance);
|
2021-11-30 14:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a zero module
|
|
|
|
|
/// </summary>
|
2021-12-12 15:41:51 +01:00
|
|
|
public void Add(Type moduleType, IZeroModule moduleInstance)
|
2021-11-30 14:32:10 +01:00
|
|
|
{
|
|
|
|
|
if (_modules.ContainsKey(moduleType))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_modules.TryAdd(moduleType, moduleInstance);
|
2021-12-12 15:41:51 +01:00
|
|
|
}
|
2021-11-30 14:32:10 +01:00
|
|
|
|
2021-12-12 15:41:51 +01:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
foreach (var module in _modules.OrderBy(x => x.Value.Order))
|
|
|
|
|
{
|
|
|
|
|
module.Value.ConfigureServices(services, configuration);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
foreach (var module in _modules.OrderBy(x => x.Value.ConfigureOrder))
|
|
|
|
|
{
|
|
|
|
|
module.Value.Configure(app, routes, serviceProvider);
|
|
|
|
|
}
|
2021-11-30 14:32:10 +01:00
|
|
|
}
|
|
|
|
|
}
|