2021-11-22 14:29:22 +01:00
using FluentValidation ;
using Microsoft.AspNetCore.Hosting ;
using Microsoft.Extensions.Configuration ;
using Microsoft.Extensions.DependencyInjection ;
namespace zero ;
// TODO maybe use a middleware like Hangfire does: https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.AspNetCore/HangfireEndpointRouteBuilderExtensions.cs
public class ZeroBuilder
{
public virtual IServiceCollection Services { get ; }
public virtual IMvcBuilder Mvc { get ; }
2021-11-24 15:49:25 +01:00
readonly IConfiguration Configuration ;
readonly IZeroStartupOptions StartupOptions ;
2021-11-22 14:29:22 +01:00
public ZeroBuilder ( IServiceCollection services , IConfiguration configuration , Action < IZeroStartupOptions > setupAction )
{
Services = services ;
Mvc = services . AddMvc ();
Configuration = configuration ;
// create startup options
StartupOptions = new ZeroStartupOptions ( Mvc );
StartupOptions . AssemblyDiscoveryRules . Add ( new ZeroAssemblyDiscoveryRule ());
setupAction ?. Invoke ( StartupOptions );
2021-11-29 00:38:55 +01:00
services . AddControllers ();
2021-11-22 14:29:22 +01:00
// adds and discovers additional and built-in assemblies
new AssemblyDiscovery ( Mvc ). Execute ( StartupOptions . AssemblyDiscoveryRules );
2021-11-30 14:32:10 +01:00
AddModule < ConfigurationModule >();
AddModule < ContextModule >();
AddModule < ArchitectureModule >();
AddModule < CommunicationModule >();
AddModule < IdentityModule >();
AddModule < ApplicationModule >();
AddModule < PersistenceModule >();
AddModule < StoresModule >();
AddModule < FileStorageModule >();
AddModule < MapperModule >();
AddModule < LocalizationModule >();
AddModule < RenderingModule >();
2021-11-22 14:29:22 +01:00
2021-11-30 14:32:10 +01:00
AddModule < RoutingModule >();
AddModule < MailsModule >();
AddModule < MediaModule >();
AddModule < PagesModule >();
AddModule < SpacesModule >();
2021-11-22 14:29:22 +01:00
2021-11-23 22:56:22 +01:00
//if (Environment.GetEnvironmentVariable("DOTNET_WATCH") == "1")
//{
// Mvc.AddRazorRuntimeCompilation();
//}
2021-11-22 14:29:22 +01:00
// configure FluentValidation
ValidatorOptions . Global . PropertyNameResolver = ValidatorCamelCasePropertyResolver . ResolvePropertyName ;
}
/// <summary>
/// Use specified options
/// </summary>
public ZeroBuilder WithOptions ( Action < ZeroOptions > configureOptions )
{
Services . Configure ( configureOptions );
return this ;
}
2021-11-30 14:32:10 +01:00
public ZeroBuilder AddModule < T >() where T : class , IZeroModule , new ()
{
ZeroModuleCollection . AddModule < T >( Services , Configuration );
return this ;
}
2021-11-22 14:29:22 +01:00
/// <summary>
/// Adds a zero plugin
/// </summary>
public ZeroBuilder AddPlugin < T >() where T : class , IZeroPlugin , new ()
{
ZeroPluginInitializer . AddPlugin < T >( Services , Configuration );
return this ;
}
/// <summary>
/// Adds a zero plugin
/// </summary>
public ZeroBuilder AddPlugin < T >( Func < IServiceProvider , T > implementationFactory ) where T : class , IZeroPlugin , new ()
{
ZeroPluginInitializer . AddPlugin < T >( Services , Configuration , implementationFactory );
return this ;
}
}