public StoveNHibernateInterceptor(IScopeResolver scopeResolver) { IScopeResolver innerResolver = scopeResolver; _stoveSession = new Lazy <IStoveSession>( () => innerResolver.IsRegistered(typeof(IStoveSession)) ? innerResolver.Resolve <IStoveSession>() : NullStoveSession.Instance, true ); _guidGenerator = new Lazy <IGuidGenerator>( () => innerResolver.IsRegistered(typeof(IGuidGenerator)) ? innerResolver.Resolve <IGuidGenerator>() : SequentialGuidGenerator.Instance, true ); _eventBus = new Lazy <IEventBus>( () => innerResolver.IsRegistered(typeof(IEventBus)) ? innerResolver.Resolve <IEventBus>() : NullEventBus.Instance, true ); _commandContextAccessor = new Lazy <IStoveCommandContextAccessor>( () => innerResolver.Resolve <IStoveCommandContextAccessor>(), true); }
public StoveNHibernateInterceptor(IScopeResolver scopeResolver) { _scopeResolver = scopeResolver; _stoveSession = new Lazy <IStoveSession>( () => _scopeResolver.IsRegistered(typeof(IStoveSession)) ? _scopeResolver.Resolve <IStoveSession>() : NullStoveSession.Instance, isThreadSafe: true ); _guidGenerator = new Lazy <IGuidGenerator>( () => _scopeResolver.IsRegistered(typeof(IGuidGenerator)) ? _scopeResolver.Resolve <IGuidGenerator>() : SequentialGuidGenerator.Instance, isThreadSafe: true ); _eventBus = new Lazy <IEventBus>( () => _scopeResolver.IsRegistered(typeof(IEventBus)) ? _scopeResolver.Resolve <IEventBus>() : NullEventBus.Instance, isThreadSafe: true ); }
public TDbContext Resolve <TDbContext>(string connectionString) { Type dbContextType = GetConcreteType <TDbContext>(); return((TDbContext)_scopeResolver.Resolve(dbContextType, new { nameOrConnectionString = connectionString })); }
public IUnitOfWorkCompleteHandle Begin(UnitOfWorkOptions options) { _childScope = _scopeResolver.BeginScope(); options.FillDefaultsForNonProvidedOptions(_defaultOptions); IUnitOfWork outerUow = _currentUnitOfWorkProvider.Current; if (options.Scope == TransactionScopeOption.Required && outerUow != null) { return(new InnerUnitOfWorkCompleteHandle()); } var uow = _childScope.Resolve <IUnitOfWork>(); uow.Completed += (sender, args) => { _currentUnitOfWorkProvider.Current = null; }; uow.Failed += (sender, args) => { _currentUnitOfWorkProvider.Current = null; }; uow.Disposed += (sender, args) => { _childScope.Dispose(); }; //Inherit filters from outer UOW if (outerUow != null) { options.FillOuterUowFiltersForNonProvidedOptions(outerUow.Filters.ToList()); } uow.Begin(options); _currentUnitOfWorkProvider.Current = uow; return(uow); }
public void Consume() { var conf = new StoveDbContextConfiguration <ContextBoundObject>("A", null); _scope.Resolve <IStoveDbContextConfigurer <ContextBoundObject> >().Configure(conf); conf.ConnectionString.Should().Be("b"); }
private DbContext GetDbContext(ActiveTransactionProviderArgs args) { var dbContextType = (Type)args["ContextType"]; Type dbContextProviderType = typeof(IDbContextProvider <>).MakeGenericType(dbContextType); object dbContextProvider = _scopeResolver.Resolve(dbContextProviderType); var dbContext = getDbContextMethod.Invoke(dbContextProvider, null).As <DbContext>(); return(dbContext); }
private ISession GetSession(ActiveTransactionProviderArgs args) { var sessionContextType = (Type)args["SessionContextType"]; var sessionContextProvider = _scope.Resolve <ISessionProvider>(); MethodInfo method = getSessionMethod.MakeGenericMethod(sessionContextType); var session = method.Invoke(sessionContextProvider, null).As <ISession>(); return(session); }
public void Migrate(Action <string> logger = null) { var dbContext = _resolver.Resolve <TDbContext>(); CreateVersionInfoTableIfNotExists(dbContext, logger); List <VersionInfo> existingVersionInfos = GetVersionInfoTableContent(dbContext, logger); foreach (IStoveMigration migration in _stoveMigrations) { StoveVersionInfoAttribute[] stoveVersionInfoAttributes = migration.GetType().GetCustomAttributes <StoveVersionInfoAttribute>().ToArray(); if (!stoveVersionInfoAttributes.Any()) { logger?.Invoke($"No version attribute found in migration file."); continue; } StoveVersionInfoAttribute stoveVersionInfoAttribute = stoveVersionInfoAttributes.First(); var enviromentAttribute = migration.GetType().GetCustomAttribute <EnviromentAttribute>(); if (stoveVersionInfoAttribute != null && existingVersionInfos.All(x => x.Version != stoveVersionInfoAttribute.GetVersion())) { try { if (_configuration.Enviroment != null) { if (enviromentAttribute != null && !enviromentAttribute.IsValidEnviroment(_configuration.Enviroment)) { logger?.Invoke($"Enviroment is not valid for this migration."); continue; } } bool workWithoutEnvironment = enviromentAttribute == null && _configuration.Enviroment.IsNullOrEmpty(); bool workWithEnvironment = enviromentAttribute != null && !_configuration.Enviroment.IsNullOrEmpty() && enviromentAttribute.IsValidEnviroment(_configuration.Enviroment); if (workWithoutEnvironment || workWithEnvironment) { var versionInfo = new VersionInfo(stoveVersionInfoAttribute.GetVersion(), Clock.Now, $"Author: {stoveVersionInfoAttribute.GetAuthor()}, Description: {stoveVersionInfoAttribute.GetDescription()}" ); logger?.Invoke($"Migration is runing with following VersionInfo details: Version: {versionInfo.Version} AppliedOn: {versionInfo.AppliedOn} {versionInfo.Description}"); migration.Execute(); InsertNewVersionInfoToVersionInfoTable(dbContext, versionInfo, logger); } } catch (Exception exception) { logger?.Invoke($"An error occured while migration is runing: {exception}"); throw; } } } }
public void Migrate <TDbContext, TConfiguration>(string nameOrConnectionString, Assembly migrationAssembly, Action <string> logger) where TDbContext : DbContext where TConfiguration : DbMigrationsConfiguration <TDbContext>, new() { logger($"MigrationStrategy: DbContext starting for {typeof(TDbContext).GetTypeInfo().Name}..."); using (IScopeResolver scope = _resolver.BeginScope()) { var dbInitializer = scope.Resolve <StoveDbContextMigration <TDbContext> >(); dbInitializer.Migrate(logger); } logger($"MigrationStrategy: DbContext finished succesfully for {typeof(TDbContext).GetTypeInfo().Name}."); }
public void Migrate <TDbContext, TConfiguration>(string nameOrConnectionString, Assembly migrationAssembly, Action <string> logger) where TDbContext : DbContext where TConfiguration : DbMigrationsConfiguration <TDbContext>, new() { logger($"MigrationStrategy: DbContext starting for {typeof(TDbContext).GetTypeInfo().Name}..."); using (IScopeResolver scope = _resolver.BeginScope()) { var dbContext = scope.Resolve <TDbContext>(new { nameOrConnectionString }); var dbInitializer = new MigrateDatabaseToLatestVersion <TDbContext, TConfiguration>(true, new TConfiguration()); dbInitializer.InitializeDatabase(dbContext); } logger($"MigrationStrategy: DbContext finished succesfully for {typeof(TDbContext).GetTypeInfo().Name}."); }
/// <summary> /// Nolockings the specified queryable. /// </summary> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <typeparam name="TPrimaryKey">The type of the primary key.</typeparam> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="repository">The repository.</param> /// <param name="queryable">The queryable.</param> /// <returns></returns> public static TResult Nolocking <TEntity, TPrimaryKey, TResult>(this IRepository <TEntity, TPrimaryKey> repository, Func <IQueryable <TEntity>, TResult> queryable) where TEntity : class, IEntity <TPrimaryKey> { Check.NotNull(queryable, nameof(queryable)); TResult result; using (IScopeResolver scopeResolver = repository.As <IStoveRepositoryBaseWithResolver>().ScopeResolver.BeginScope()) { using (scopeResolver.Resolve <WithNoLockInterceptor>().UseNolocking()) { result = queryable(repository.GetAll()); } } return(result); }
public RegistrationCompletedEventTests() { SampleClass sampleClassInstance = null; Building(builder => { builder.RegisterServices(r => r.RegisterType <SampleClass>()); builder.RegisterServices(r => r.RegistrationCompleted += (sender, args) => { var scopeResolver = args.Resolver.Resolve <IScopeResolver>(); using (IScopeResolver scope = scopeResolver.BeginScope()) { sampleClassInstance = scope.Resolve <SampleClass>(); } }); }); sampleClassInstance.DisposeCount.Should().Be(1); }
/// <summary> /// Resolves this instance. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public T Resolve <T>() { return(_scope.Resolve <T>()); }
public void ExecuteCreationAuditFilter <TEntity, TPrimaryKey>(TEntity entity) where TEntity : class, IEntity <TPrimaryKey> { var filter = _scopeResolver.Resolve <CreationAuditActionFilter>(); filter.ExecuteFilter <TEntity, TPrimaryKey>(entity); }
/// <summary> /// Resolves handler object from Ioc container. /// </summary> /// <returns>Resolved handler object</returns> public IEventHandler GetHandler() { _childScope = _scopeResolver.BeginScope(); return((IEventHandler)_childScope.Resolve(HandlerType)); }