Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Services = services;
            var connection = "Filename=db.sqlite";//Configuration.GetConnectionString("SQLite");

            Services.AddEntityFramework()
            .AddDbContext <RepositoryContext>(
                options => options.UseSqlite(connection)
                );

            Services.AddMvc();
            Services.AddMemoryCache();

            //Scoped services are injected into constructors that match the interface
            foreach (var t in ModelService.AllModels())
            {
                Type unbound_interface = typeof(IRepository <>); //Interface for type construction
                Type unbound_instance  = typeof(Repository <>);  //Constructed service class for dependency injection
                Services.AddScoped(
                    unbound_interface.MakeGenericType(t),        //Bind reflected model type to interface
                    unbound_instance.MakeGenericType(t)          //Bind reflected model type to service
                    );
            }


            //Singleton services are created on first injection and re-used
            var scheduler = Chroniton.Singularity.Instance; //Background task scheduler

            Services.AddSingleton <Chroniton.ISingularity>(scheduler);
            scheduler.Start();
        }
Example #2
0
 public RepositoryContext(DbContextOptions <RepositoryContext> options) : base(options)
 {
     ModelTypes = ModelService.AllModels();
 }