public IClaptrapBootstrapper Build() { try { return(BuildCore()); } catch (Exception e) { _logger.LogError(e, _l.Value[LK.failed_to_build_claptrap_bootstrapper]); throw; } IClaptrapBootstrapper BuildCore() { var container = CreateContainerForScanning(); using var storeScope = container.BeginLifetimeScope(); var factory = storeScope.Resolve <IClaptrapDesignStoreFactory>(); var store = CreateDefaultStore(factory, Options.DesignTypes ?? throw new ArgumentNullException()); // minion can save nothing this.ConfigureClaptrapDesign(x => x.IsMinion(), x => { x.EventSaverFactoryType = typeof(EmptyEventSaverFactory); x.ClaptrapStorageProviderOptions.EventSaverOptions = new EmptyEventSaverOptions(); }); IEnumerable <Module> appAutofacModules = Enumerable.Empty <Module>(); var(appProviderScope, appProviders) = CreateAppProviders(container, store); using (appProviderScope) { var claptrapDesignStoreConfigurators = appProviders.SelectMany(x => x.GetClaptrapDesignStoreConfigurators()); var allConfig = claptrapDesignStoreConfigurators.Concat(Options.ClaptrapDesignStoreConfigurators); store = ConfigStore(store, allConfig); var validator = storeScope.Resolve <IClaptrapDesignStoreValidator>(); store = ValidateStore(store, validator); var appModules = appProviders .SelectMany(x => { var ms = x.GetClaptrapApplicationModules().ToArray(); _logger.LogDebug("Found {count} claptrap application modules from {type} : {modules}", ms.Length, x, ms.Select(a => a.Name)); return(ms); }) .ToArray(); _logger.LogInformation( "Scanned {typesCount}, and found {count} claptrap application modules : {modules}", Options.ModuleTypes.Count(), appModules.Length, appModules.Select(x => x.Name)); appAutofacModules = appModules .OfType <Module>() .ToArray(); _logger.LogInformation( "Filtered and found {count} Autofac modules : {modules}", appAutofacModules.Count(), appAutofacModules); } var providers = ScanClaptrapModuleProviders(); _applicationBuilder.RegisterTypes(providers) .As <IClaptrapModuleProvider>(); var claptrapBootstrapper = new AutofacClaptrapBootstrapper(_applicationBuilder, appAutofacModules .Concat(new[] { new ClaptrapBootstrapperBuilderOptionsModule(Options) }), store); return(claptrapBootstrapper); } }
public IClaptrapBootstrapper Build() { try { return(BuildCore()); } catch (Exception e) { _logger.LogError(e, _l.Value[LK.failed_to_build_claptrap_bootstrapper]); throw; } IClaptrapBootstrapper BuildCore() { var container = CreateContainerForScanning(); IClaptrapDesignStore?claptrapDesignStore = null; // minion can save nothing this.ConfigureClaptrapDesign(x => x.IsMinion(), x => { x.EventSaverFactoryType = typeof(EmptyEventSaverFactory); x.ClaptrapStorageProviderOptions.EventSaverOptions = new EmptyEventSaverOptions(); }); claptrapDesignStore = CreateClaptrapDesignStore(container); IClaptrapApplicationModule[]? applicationModules = null; applicationModules = ScanApplicationModules(container, claptrapDesignStore); var autofacModules = applicationModules .OfType <Module>() .ToArray(); _logger.LogInformation( "Filtered and found {count} Autofac modules : {modules}", autofacModules.Length, autofacModules); var providers = ScanClaptrapModuleProviders(); _applicationBuilder.RegisterTypes(providers) .As <IClaptrapModuleProvider>(); var claptrapBootstrapper = new AutofacClaptrapBootstrapper(_applicationBuilder, autofacModules .Concat(new[] { new ClaptrapBootstrapperBuilderOptionsModule(Options) }), claptrapDesignStore); return(claptrapBootstrapper); } IContainer CreateContainerForScanning() { var builder = new ContainerBuilder(); builder.RegisterModule <ClaptrapDesignScanningModule>(); builder.RegisterModule(new LoggingModule(_loggerFactory)); builder.RegisterModule(new LocalizationModule(Options.ClaptrapLocalizationOptions)); builder.RegisterInstance(Options); var container = builder.Build(); return(container); } IClaptrapDesignStore CreateClaptrapDesignStore(ILifetimeScope container) { using var scope = container.BeginLifetimeScope(); var factory = scope.Resolve <IClaptrapDesignStoreFactory>(); var validator = scope.Resolve <IClaptrapDesignStoreValidator>(); var claptrapDesignStore = this.CreateClaptrapDesignStore( factory, validator, Options.DesignTypes ?? throw new ArgumentNullException()); return(claptrapDesignStore); } IClaptrapApplicationModule[] ScanApplicationModules(ILifetimeScope container, IClaptrapDesignStore claptrapDesignStore) { using var scope = container.BeginLifetimeScope(innerBuilder => { var providerTypes = Options.ModuleTypes .Where(x => x.IsClass && !x.IsAbstract) .Where(x => x.GetInterface(typeof(IClaptrapApplicationModulesProvider).FullName) != null) .ToArray(); _logger.LogDebug("Found type {providerTypes} as {name}", providerTypes, nameof(IClaptrapApplicationModulesProvider)); innerBuilder.RegisterTypes(providerTypes) .As <IClaptrapApplicationModulesProvider>() .InstancePerLifetimeScope(); innerBuilder.RegisterInstance(claptrapDesignStore); }); var moduleProviders = scope.Resolve <IEnumerable <IClaptrapApplicationModulesProvider> >(); var applicationModules = moduleProviders .SelectMany(x => { var ms = x.GetClaptrapApplicationModules().ToArray(); _logger.LogDebug("Found {count} claptrap application modules from {type} : {modules}", ms.Length, x, ms.Select(a => a.Name)); return(ms); }) .ToArray(); _logger.LogInformation( "Scanned {typesCount}, and found {count} claptrap application modules : {modules}", Options.ModuleTypes.Count(), applicationModules.Length, applicationModules.Select(x => x.Name)); return(applicationModules); } Type[] ScanClaptrapModuleProviders() { var providers = Options.ModuleTypes .Where(x => x.IsClass && !x.IsAbstract) .Where(x => x.GetInterface(typeof(IClaptrapModuleProvider).FullName) != null) .ToArray(); _logger.LogInformation( "Scanned {typeCount}, and found {count} claptrap modules providers : {modules}", Options.ModuleTypes.Count(), providers.Length, providers); return(providers); } }