public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            //TODO: Implement better logger handling.
            loggerFactory.AddConsole(LogLevel.Information);

            IOptions <ModuleSystemOptions> options = app.ApplicationServices.GetService <IOptions <ModuleSystemOptions> >();

            if (options.Value.ApplicationConfigurationModulesPaths != null)
            {
                foreach (string path in options.Value.ApplicationConfigurationModulesPaths)
                {
                    //Only one should be in the app.
                    //ORDER MATTERS!
                    ModuleLoader loader = new ModuleLoader(path);

                    IRegisterModule module = Activator.CreateInstance(loader.GetType <ApplicationConfigurationModule>(), new object[] { app, loggerFactory }) as IRegisterModule;

                    if (module == null)
                    {
                        throw new InvalidOperationException($"Failed to create an instance for derived {nameof(ApplicationConfigurationModule)} Type: {loader.GetType<ApplicationConfigurationModule>().FullName}.");
                    }

                    //Register the module.
                    module.Register();
                }
            }

            //Register MVC last in the pipeline. NO OTHER MODULE SHOULD REGISTER THIS!
            app.UseMvc();
        }
 public Controller(IUserInterface ui, IConditionerData database, IRegisterModule registerModule, ITestingModule testingModule,
                   ISearchingModule searchingModule, IValidityModule validityModule, IStatusModule statusModule)
 {
     this.ui              = ui;
     this.database        = database;
     this.registerModule  = registerModule;
     this.testingModule   = testingModule;
     this.searchingModule = searchingModule;
     this.validityModule  = validityModule;
     this.statusModule    = statusModule;
 }
 public Controller(IUserInterface ui, IConditionerData database, IRegisterModule registerModule, ITestingModule testingModule, 
     ISearchingModule searchingModule, IValidityModule validityModule, IStatusModule statusModule)
 {
     this.ui = ui;
     this.database = database;
     this.registerModule = registerModule;
     this.testingModule = testingModule;
     this.searchingModule = searchingModule;
     this.validityModule = validityModule;
     this.statusModule = statusModule;
 }
예제 #4
0
 public CHIP8(Func <bool[, ], Task> writeDisplay,
              IRegisterModule registerModule,
              IStackModule stackModule,
              MemoryModule memoryModule,
              IRandomModule randomModule)
 {
     updateDisplay       = writeDisplay;
     this.registerModule = registerModule;
     this.stackModule    = stackModule;
     this.ram            = memoryModule;
     this.random         = randomModule;
 }
예제 #5
0
 public static CHIP8 GetChip8(Func <bool[, ], Task> writeDisplay = null,
                              IRegisterModule registers          = null,
                              IStackModule stack   = null,
                              MemoryModule mem     = null,
                              IRandomModule random = null)
 {
     return(new CHIP8(writeDisplay ?? (x => Task.CompletedTask),
                      registers ?? new RegisterModule(),
                      stack ?? new StackModule(),
                      mem
                      ?? new MemoryModule(Enumerable.Repeat((byte)0x0,
                                                            4096)),
                      random ?? new RandomModule()));
 }
        public virtual void ConfigureServices(IServiceCollection services)
        {
            services.AddLogging();
            services.AddOptions();

            //configures the JSON DTO for the module options and database options
            services.Configure <ModuleSystemOptions>(Configuration.GetSection(nameof(ModuleSystemOptions)));

            services.Configure <DatabaseConfigOptions>(Configuration.GetSection(nameof(DatabaseConfigOptions)));

            //temporarily generate a service provider
            IServiceProvider provider = services.BuildServiceProvider();

            IOptions <ModuleSystemOptions>   moduleOptions   = provider.GetService <IOptions <ModuleSystemOptions> >();
            IOptions <DatabaseConfigOptions> databaseOptions = provider.GetService <IOptions <DatabaseConfigOptions> >();

            if (moduleOptions?.Value == null)
            {
                throw new InvalidOperationException($"Couldn't create {nameof(ModuleSystemOptions)} from modules.json.");
            }

            if (databaseOptions == null || databaseOptions.Value == null)
            {
                throw new InvalidOperationException($"Couldn't create {nameof(DatabaseConfigOptions)} from database.json.");
            }

            //TODO: Add support for multiple DB providers
            //Add DB services depending on the config
            if (databaseOptions.Value.useInMemoryDatabase)
            {
                services.AddEntityFrameworkInMemoryDatabase();
            }
            else
            {
                services.AddEntityFrameworkMySql();
            }

            //Register Mvc services and aquire builder
            IMvcBuilder mvcBuilder = services.AddMvc();

            try
            {
                //This means we have no services that need registering
                if (moduleOptions.Value.ServiceRegistrationModulesPaths == null)
                {
                    return;
                }

                foreach (string path in moduleOptions.Value.ServiceRegistrationModulesPaths)
                {
                    ModuleLoader loader = new ModuleLoader(path);

                    foreach (Type serviceRegisterType in loader.GetTypes <ServiceRegistrationModule>())
                    {
                        IRegisterModule module = Activator.CreateInstance(serviceRegisterType, new object[] { services, (Action <DbContextOptionsBuilder>)((DbContextOptionsBuilder options) =>
                            {
                                if (databaseOptions.Value.useInMemoryDatabase)
                                {
                                    options.UseInMemoryDatabase();
                                }
                                else
                                {
                                    options.UseMySql(databaseOptions.Value.DatabaseConnectionString);
                                }
                            }) }) as IRegisterModule;

                        if (module == null)
                        {
                            throw new InvalidOperationException($"Failed to create an instance for derived {nameof(ServiceRegistrationModule)} Type: {serviceRegisterType.FullName}.");
                        }

                        //Register the module.
                        module.Register();
                    }

                    foreach (Type serviceRegisterType in loader.GetTypes <MvcBuilderServiceRegistrationModule>())
                    {
                        IRegisterModule module = Activator.CreateInstance(serviceRegisterType, new object[] { mvcBuilder }) as IRegisterModule;

                        if (module == null)
                        {
                            throw new InvalidOperationException($"Failed to create an instance for derived {nameof(MvcBuilderServiceRegistrationModule)} Type: {serviceRegisterType.FullName}.");
                        }

                        //Register the module.
                        module.Register();
                    }
                }
            }
            catch (Exception e)
            {
                throw new InvalidOperationException($"Encountered Exception in Module registration: {e.Message}", e);
            }
        }