public static IServiceCollection AddDatabaseConnection <T>(this IServiceCollection services, string connectionString, ConnectionScope scope) where T : class, IConnectionFactory, new() { _container = _container ?? new HarmonyContainer(services.BuildServiceProvider()); _container.AddAspNetCore(); return(AddDatabaseConnection <T>(services, connectionString, scope, "Default")); }
public static IServiceCollection AddDatabaseConnection <T>(this IServiceCollection services, string connectionString, ConnectionScope scope, string slot, Action <IDbConnection> onConnection = null, Action <IDbCommand, Type> onCommand = null) where T : class, IConnectionFactory, new() { var factory = new T { ConnectionString = connectionString }; services.AddTransient(r => _container.Resolve <T>(slot)); services.AddTransient(r => _container.Resolve <DataContext>(slot)); services.AddTransient(delegate { return(_container.Resolve <IDataConnection>(slot)); }); _container = _container ?? new HarmonyContainer(services.BuildServiceProvider()); _container.AddAspNetCore(); _container.Register(slot, r => factory, Lifetime.Permanent); switch (scope) { case ConnectionScope.AlwaysNew: _container.Register(slot, r => new DataContext(r.Resolve <T>(slot), onConnection)); _container.Register <IDataConnection>(slot, r => new DataConnection(r.Resolve <DataContext>(slot), onCommand)); break; case ConnectionScope.ByRequest: _container.Register(slot, r => new DataContext(r.Resolve <T>(slot), onConnection), Lifetime.Request); _container.Register <IDataConnection>(slot, r => new DataConnection(r.Resolve <DataContext>(slot), onCommand), Lifetime.Request); break; case ConnectionScope.ByThread: _container.Register(slot, r => new DataContext(r.Resolve <T>(slot), onConnection), Lifetime.Thread); _container.Register <IDataConnection>(slot, r => new DataConnection(r.Resolve <DataContext>(slot), onCommand), Lifetime.Thread); break; case ConnectionScope.KeepAlive: _container.Register(slot, r => new DataContext(r.Resolve <T>(slot), onConnection), Lifetime.Permanent); _container.Register <IDataConnection>(slot, r => new DataConnection(r.Resolve <DataContext>(slot), onCommand), Lifetime.Permanent); break; default: throw new ArgumentOutOfRangeException(nameof(scope), scope, null); } return(services); }
public static IServiceCollection AddDatabaseConnection <T>(IServiceCollection services, string connectionString, Func <IConnectionFactory, DataContext> scope, string slot) where T : class, IConnectionFactory, new() { var factory = new T { ConnectionString = connectionString }; services.AddTransient(r => _container.Resolve <T>(slot)); services.AddTransient(r => _container.Resolve <DataContext>(slot)); _container = _container ?? new HarmonyContainer(services.BuildServiceProvider()); _container.AddAspNetCore(); _container.Register(slot, r => factory, Lifetime.Permanent); _container.Register(slot, r => scope(r.Resolve <IConnectionFactory>())); return(services); }
public static IServiceCollection AddDatabaseConnection <TScope, TConnectionFactory>( this IServiceCollection services, string connectionString, ConnectionScope scope, Action <IDbConnection> onConnection = null, Action <IDbCommand, Type> onCommand = null) where TConnectionFactory : class, IConnectionFactory, new() { services.AddTransient(r => _container.Resolve <IDataConnection <TScope> >()); _container = _container ?? new HarmonyContainer(services.BuildServiceProvider()); _container.AddAspNetCore(); var slot = $"{typeof(TScope).FullName}"; AddDatabaseConnection <TConnectionFactory>(services, connectionString, scope, slot, onConnection, onCommand); switch (scope) { case ConnectionScope.AlwaysNew: _container.Register <IDataConnection <TScope> >(r => new DataConnection <TScope>(r.Resolve <DataContext>(slot), onCommand)); break; case ConnectionScope.ByRequest: _container.Register <IDataConnection <TScope> >( r => new DataConnection <TScope>(r.Resolve <DataContext>(slot), onCommand), Lifetime.Request); break; case ConnectionScope.ByThread: _container.Register <IDataConnection <TScope> >( r => new DataConnection <TScope>(r.Resolve <DataContext>(slot), onCommand), Lifetime.Thread); break; case ConnectionScope.KeepAlive: _container.Register <IDataConnection <TScope> >( r => new DataConnection <TScope>(r.Resolve <DataContext>(slot), onCommand), Lifetime.Permanent); break; default: throw new ArgumentOutOfRangeException(nameof(scope), scope, null); } return(services); }
private static IWebHostBuilder Create ( ICollection <Action <IConfiguration> > configuration, ICollection <Action <IServiceCollection> > configureServices, ICollection <Action <IApplicationBuilder> > configure ) { T startup = null; IConfiguration config = null; IHostingEnvironment env; IServiceCollection services = null; var builder = new WebHostBuilder() .ConfigureAppConfiguration((context, cb) => { env = context.HostingEnvironment; var testSettings = BuildTestSettings(env.ContentRootPath, env.EnvironmentName); ConfigureAppConfiguration(env, testSettings, cb); config = cb.Build(); }) .ConfigureServices(serviceCollection => { services = serviceCollection; Debug.Assert(services != null); Debug.Assert(config != null); var serviceProvider = serviceCollection.BuildServiceProvider(); Debug.Assert(serviceProvider != null); using (var container = new HarmonyContainer(serviceProvider)) { container.AddAspNetCore(); container.Register(serviceProvider); container.Register(services); container.Register(config); startup = container.Resolve <T>(); Debug.Assert(startup != null); foreach (var action in configuration) { action(config); } container.Register(startup); container.InvokeMethod <T>("ConfigureServices"); foreach (var action in configureServices) { action(serviceCollection); } } }) .Configure(app => { Debug.Assert(services != null); Debug.Assert(config != null); Debug.Assert(startup != null); Debug.Assert(app != null); var serviceProvider = services.BuildServiceProvider(); Debug.Assert(serviceProvider != null); using (var container = new HarmonyContainer(serviceProvider)) { container.AddAspNetCore(); container.Register(serviceProvider); container.Register(services); container.Register(config); container.Register(startup); container.Register(app); container.InvokeMethod <T>("Configure"); foreach (var action in configure) { action(app); } } }); var applicationKey = builder.GetSetting(WebHostDefaults.ApplicationKey); var assemblyName = applicationKey ?? typeof(T).Assembly.GetName().Name; if (applicationKey != assemblyName) { builder.UseSetting(WebHostDefaults.ApplicationKey, assemblyName); } return(builder); }
public HarmonyContainerFixture() { C = new HarmonyContainer(); }