You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.4 KiB
C#

12 months ago
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace DS.Module.Core.Modules;
public static class AppModuleExtensions
{
public static IServiceCollection AddApplication<T>(this IServiceCollection services) where T : IDSAppModule
{
services.AddApplication(typeof(T));
return services;
}
private static IServiceCollection AddApplication(this IServiceCollection services, Type type)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
var obj = new ObjectAccessor<IApplicationBuilder>();
services.Add(ServiceDescriptor.Singleton(typeof(ObjectAccessor<IApplicationBuilder>), obj));
services.Add(ServiceDescriptor.Singleton(typeof(IObjectAccessor<IApplicationBuilder>), obj));
IStartupModuleRunner runner = new StartupModuleRunner(type, services);
runner.ConfigureServices(services);
return services;
}
public static IApplicationBuilder InitializeApplication(this IApplicationBuilder builder)
{
builder.ApplicationServices.GetRequiredService<ObjectAccessor<IApplicationBuilder>>().Value = builder;
var runner = builder.ApplicationServices.GetRequiredService<IStartupModuleRunner>();
runner.Initialize(builder.ApplicationServices);
return builder;
}
}