예제 #1
0
        public IServiceCollection Bootstrap(IServiceCollection services)
        {
            var config    = services.BuildServiceProvider().GetRequiredService <IConfiguration>();
            var sqlConfig = new SQLPluginConfig();

            config.Bind("SQLPlugin", sqlConfig);

            services.AddScoped <DbConnection>((serviceProvider) =>
            {
                // using Microsoft.Data.SqlClient
                var dbConnection = new SqlConnection(sqlConfig.ConnectionString);
                dbConnection.Open();
                return(dbConnection);
            });

            services.AddScoped <DbContextOptions>((serviceProvider) =>
            {
                var dbConnection = serviceProvider.GetService <DbConnection>();
                return(new DbContextOptionsBuilder <ProductsDbContext>()
                       .UseSqlServer(dbConnection)
                       .Options);
            });

            services.AddScoped <ProductsDbContext>((serviceProvider) =>
            {
                var options = serviceProvider.GetService <DbContextOptions>();
                var context = new ProductsDbContext(options);
                return(context);
            });

            return(services);
        }
        public SqlProductsRepository(IServiceProvider serviceProvider)
        {
            var config    = serviceProvider.GetRequiredService <IConfiguration>();
            var sqlConfig = new SQLPluginConfig();

            config.Bind("SQLPlugin", sqlConfig);
            var options = new DbContextOptionsBuilder <ProductsDbContext>()
                          .UseSqlServer(sqlConfig.ConnectionString)
                          .Options;

            this.dbContext = new ProductsDbContext(options);
        }
 internal SqlProductsRepository(ProductsDbContext dbContext)
 {
     this.dbContext = dbContext;
 }