using FluentValidation; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System.IO; 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; } IConfiguration Configuration; IZeroStartupOptions StartupOptions; public ZeroBuilder(IServiceCollection services, IConfiguration configuration, Action setupAction) { ZeroModuleInitializer.RegisterAll(new ZeroModuleConfiguration(services, configuration)); Services = services; Mvc = services.AddMvc(); Configuration = configuration; // create startup options StartupOptions = new ZeroStartupOptions(Mvc); StartupOptions.AssemblyDiscoveryRules.Add(new ZeroAssemblyDiscoveryRule()); setupAction?.Invoke(StartupOptions); // adds and discovers additional and built-in assemblies new AssemblyDiscovery(Mvc).Execute(StartupOptions.AssemblyDiscoveryRules); // add default plugin AddPlugin(); Mvc.AddNewtonsoftJson(x => { // TODO this shall only be configurated for backoffice controllers BackofficeJsonSerlializerSettings.Setup(x.SerializerSettings); }); if (Environment.GetEnvironmentVariable("DOTNET_WATCH") == "1") { Mvc.AddRazorRuntimeCompilation(); } // configure FluentValidation ValidatorOptions.Global.PropertyNameResolver = ValidatorCamelCasePropertyResolver.ResolvePropertyName; // add default services Services.AddScoped(); Services.AddScoped(); Services.AddScoped(); //Services.AddScoped(); Services.AddTransient(); Services.AddScoped(factory => new Paths(factory.GetService(), true)); // add dev server Services.AddOptions() .Bind(Configuration.GetSection("Zero:DevServer")) .Configure((opts, env) => { opts.WorkingDirectory = Path.Combine(env.ContentRootPath, "..", "Zero.Web.UI", "App"); }); Services.AddHostedService(); } /// /// Use specified options /// public ZeroBuilder WithOptions(Action configureOptions) { Services.Configure(configureOptions); return this; } /// /// Adds a zero plugin /// public ZeroBuilder AddPlugin() where T : class, IZeroPlugin, new() { ZeroPluginInitializer.AddPlugin(Services, Configuration); return this; } /// /// Adds a zero plugin /// public ZeroBuilder AddPlugin(Func implementationFactory) where T : class, IZeroPlugin, new() { ZeroPluginInitializer.AddPlugin(Services, Configuration, implementationFactory); return this; } }