public static IServiceCollection AddDatabaseConnection <T>(this IServiceCollection services, string connectionString, ConnectionScope scope) where T : class, IConnectionFactory, new() { _container = _container ?? new DependencyContainer(services.BuildServiceProvider()); _container.AddAspNetCore(); return(AddDatabaseConnection <T>(services, connectionString, scope, Constants.ConnectionSlots.Default)); }
public static IServiceCollection AddDatabaseConnection <TScope, TConnectionFactory>( this IServiceCollection services, string connectionString, ConnectionScope scope, Action <IDbConnection, IServiceProvider> onConnection = null, Action <IDbCommand, Type, IServiceProvider> onCommand = null) where TConnectionFactory : class, IConnectionFactory, new() { services.AddTransient(r => _container.Resolve <IDataConnection <TScope> >()); var serviceProvider = services.BuildServiceProvider(); _container = _container ?? new DependencyContainer(serviceProvider); _container.AddAspNetCore(); _container.Register(r => serviceProvider); 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), r.MustResolve <IServiceProvider>(), onCommand)); break; case ConnectionScope.ByRequest: _container.Register <IDataConnection <TScope> >( r => new DataConnection <TScope>(r.Resolve <DataContext>(slot), r.MustResolve <IServiceProvider>(), onCommand), Lifetime.Request); break; case ConnectionScope.ByThread: _container.Register <IDataConnection <TScope> >( r => new DataConnection <TScope>(r.Resolve <DataContext>(slot), r.MustResolve <IServiceProvider>(), onCommand), Lifetime.Thread); break; case ConnectionScope.KeepAlive: _container.Register <IDataConnection <TScope> >( r => new DataConnection <TScope>(r.Resolve <DataContext>(slot), r.MustResolve <IServiceProvider>(), 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 DependencyContainer(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 <T>(this IServiceCollection services, string connectionString, ConnectionScope scope, string slot, Action <IDbConnection, IServiceProvider> onConnection = null, Action <IDbCommand, Type, IServiceProvider> 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)); }); var serviceProvider = services.BuildServiceProvider(); _container = _container ?? new DependencyContainer(serviceProvider); _container.AddAspNetCore(); _container.Register(slot, r => factory, Lifetime.Permanent); _container.Register(slot, r => serviceProvider); switch (scope) { case ConnectionScope.AlwaysNew: _container.Register(slot, r => new DataContext(r.Resolve <T>(slot), r.Resolve <IServiceProvider>(), onConnection)); _container.Register <IDataConnection>(slot, r => new DataConnection(r.Resolve <DataContext>(slot), r.Resolve <IServiceProvider>(), onCommand)); break; case ConnectionScope.ByRequest: _container.Register(slot, r => new DataContext(r.Resolve <T>(slot), r.Resolve <IServiceProvider>(), onConnection), Lifetime.Request); _container.Register <IDataConnection>(slot, r => new DataConnection(r.Resolve <DataContext>(slot), r.Resolve <IServiceProvider>(), onCommand), Lifetime.Request); break; case ConnectionScope.ByThread: _container.Register(slot, r => new DataContext(r.Resolve <T>(slot), r.Resolve <IServiceProvider>(), onConnection), Lifetime.Thread); _container.Register <IDataConnection>(slot, r => new DataConnection(r.Resolve <DataContext>(slot), r.Resolve <IServiceProvider>(), onCommand), Lifetime.Thread); break; case ConnectionScope.KeepAlive: _container.Register(slot, r => new DataContext(r.Resolve <T>(slot), r.Resolve <IServiceProvider>(), onConnection), Lifetime.Permanent); _container.Register <IDataConnection>(slot, r => new DataConnection(r.Resolve <DataContext>(slot), r.Resolve <IServiceProvider>(), onCommand), Lifetime.Permanent); break; default: throw new ArgumentOutOfRangeException(nameof(scope), scope, null); } return(services); }