/// <summary> /// Initialize current facility /// </summary> protected override void Init() { if(IsDebugEnabled) log.Debug("Multiple UnitOfWork를 위한 Facility를 초기화 합니다."); Kernel.Register(Component.For(typeof(INHRepository<>)).ImplementedBy(typeof(NHRepository<>))); var multipleUowFactory = new NHMultipleUnitOfWorkFactory(); if(_configs == null) { if(IsDebugEnabled) log.Debug("프로그램에서 지정한 환경설정 정보가 없으므로, 환경설정 파일에서 정보를 얻습니다."); AssertConfigurations(); _configs = FacilityConfig.Children.Select(factoryConfig => new NHUnitOfWorkFacilityConfig(factoryConfig)).ToArray(); multipleUowFactory.DefaultFactoryName = _defaultFactoryName; } if(IsDebugEnabled) log.Debug("환경설정에 지정된 Factory 정보로부터, NHUnitOfWorkFactory를 생성합니다."); foreach(NHUnitOfWorkFacilityConfig config in _configs) { var nestedUnitOfWorkFactory = new NHUnitOfWorkFactory(config.NHibernateConfigurationFilename); nestedUnitOfWorkFactory.RegisterSessionFactory(CreateSessionFactory(config)); multipleUowFactory.Add(nestedUnitOfWorkFactory); } Kernel.Register(Component.For<IUnitOfWorkFactory>().Instance(multipleUowFactory)); // Kernel.AddComponentInstance<IUnitOfWorkFactory>(multipleUowFactory); if(IsDebugEnabled) log.Debug("Kernel에 NHMultipleUnitOfWorkFactory의 인스턴스를 등록했습니다."); }
public void Create_Throws_InvalidOperationException_When_No_SessionProvider_Has_Been_Set() { var factory = new NHUnitOfWorkFactory(); Assert.Throws<InvalidOperationException>( () => factory.Create() ); }
public void Create_Returns_NHUnitOfWork_Instance_When_DataContextProvider_Has_Been_Set() { NHUnitOfWorkFactory.SetSessionProvider(() => MockRepository.GenerateStub <ISession>()); var factory = new NHUnitOfWorkFactory(); var uowInstance = factory.Create(); Assert.That(uowInstance, Is.Not.Null); Assert.That(uowInstance, Is.TypeOf(typeof(NHUnitOfWork))); NHUnitOfWorkFactory.SetSessionProvider(null); }
public void Create_Returns_LinqToSqlUnitOfWork_Instance_When_DataContextProvider_Has_Been_Set() { NHUnitOfWorkFactory.SetSessionProvider(() => MockRepository.GenerateStub<ISession>()); var factory = new NHUnitOfWorkFactory(); var uowInstance = factory.Create(); Assert.That(uowInstance, Is.Not.Null); Assert.That(uowInstance, Is.TypeOf(typeof(NHUnitOfWork))); NHUnitOfWorkFactory.SetSessionProvider(null); }
public void FixtureSetup() { this.unityContainer = new UnityContainer(); IoC.Initialize( (x, y) => this.unityContainer.RegisterType(x, y), (x, y) => this.unityContainer.RegisterInstance(x, y), (x) => { return(unityContainer.Resolve(x)); }, (x) => { return(unityContainer.ResolveAll(x)); }); // Context Factory NHUnitOfWorkFactory ctxFactory = this.CreateNHContextFactory(); if (!ctxFactory.DatabaseExists()) { ctxFactory.CreateDatabase(); } ctxFactory.ValidateDatabaseSchema(); NHibernate.ISessionFactory sessionFactory = ctxFactory.CreateSessionFactory(); this.unityContainer.RegisterInstance <NHibernate.ISessionFactory>(sessionFactory); this.unityContainer.RegisterInstance <IDatabaseManager>(ctxFactory); this.unityContainer.RegisterInstance <IUnitOfWorkFactory>(ctxFactory); var configuration = IoC.GetInstance <NHibernate.Cfg.Configuration>(); AuditFlushEntityEventListener.OverrideIn(configuration); ValidateEventListener.AppendTo(configuration); AuditEventListener.AppendTo(configuration); TenantEventListener.AppendTo(configuration); var tenantId = Guid.NewGuid(); ApplicationContext.User = new TenantPrincipal(new CoreIdentity("fake", "hexa.auth", "*****@*****.**"), new string[] { }, tenantId); this.unityContainer.RegisterType <ISession, ISession>(new InjectionFactory((c) => { var session = ctxFactory.CurrentSession; var filter = session.EnableFilter("TenantFilter"); filter.SetParameter("tenantId", tenantId); return(session); })); // Repositories this.unityContainer.RegisterType <IEntityARepository, EntityANHRepository>(new PerResolveLifetimeManager()); this.unityContainer.RegisterType <IEntityBRepository, EntityBNHRepository>(new PerResolveLifetimeManager()); }
//protected ISession OrdersDomainSession { get; set; } //protected ISession HRDomainSession { get; set; } public static void Setup() { if (File.Exists("Test.sdf")) File.Delete("Test.sdf"); using (var engine = new System.Data.SqlServerCe.SqlCeEngine(ConnectionString)) { engine.CreateDatabase(); } var cnf = Fluently.Configure() .Database(MsSqlCeConfiguration.Standard .ConnectionString(((ConnectionStringBuilder cs) => cs.Is(ConnectionString))) .Dialect <MsSqlCe40Dialect>()) .Mappings(mappings => mappings.FluentMappings.AddFromAssembly(typeof(Order).Assembly).ExportTo(".")) .ExposeConfiguration(x => new SchemaExport(x).Execute(false, true, false));//, GetConnection(), null)); //var config = cnf.BuildConfiguration() // .SetProperty(NHibernateCfg.Environment.ReleaseConnections, "on_close"); OrdersDomainFactory = cnf.BuildConfiguration().BuildSessionFactory(); cnf = Fluently.Configure() .Database(MsSqlCeConfiguration.Standard .ConnectionString(((ConnectionStringBuilder cs) => cs.Is(ConnectionString))) .Dialect <MsSqlCe40Dialect>()) .Mappings(mappings => mappings.FluentMappings.AddFromAssembly(typeof(SalesPerson).Assembly).ExportTo(".")) .ExposeConfiguration(x => new SchemaExport(x).Execute(false, true, false));//, GetConnection(), null)); //config = cnf.BuildConfiguration() // .SetProperty(NHibernateCfg.Environment.ReleaseConnections, "on_close"); HRDomainFactory = cnf.BuildConfiguration().BuildSessionFactory(); //OrdersDomainSession = OrdersDomainFactory.OpenSession(GetConnection()); //HRDomainSession = HRDomainFactory.OpenSession(GetConnection()); NHUnitOfWorkFactory unitOfWorkFactory = new NHUnitOfWorkFactory(); unitOfWorkFactory.RegisterSessionFactoryProvider(() => OrdersDomainFactory); unitOfWorkFactory.RegisterSessionFactoryProvider(() => HRDomainFactory); UnitOfWorkSettings.DefaultIsolation = System.Transactions.IsolationLevel.ReadCommitted; //IUnitOfWork uow = UnitOfWorkFactory.Create(); var dependencyResolverFactory = new Mock<IDependencyResolverFactory>(); var dependencyResolver = new Mock<ICustomDependencyResolver>(); var logFacotry = new Mock<ILogFactory>(); var cacheManager = new Mock<ICacheManager>(); dependencyResolverFactory.Setup(x => x.CreateInstance()).Returns(dependencyResolver.Object); dependencyResolver.Setup(x => x.GetService(typeof(ILogFactory))).Returns(logFacotry.Object); dependencyResolver.Setup(x => x.GetService(typeof(ICacheManager), It.IsAny<string>())).Returns(cacheManager.Object); dependencyResolver.Setup(x => x.GetService(typeof(IUnitOfWorkFactory))).Returns(unitOfWorkFactory); //cacheManager.Setup(x => x.Get<IUnitOfWork>(It.IsAny<string>())).Returns(uow); IoC.InitializeWith(dependencyResolverFactory.Object); App.Data.TransactionManager transactionManager = new Data.TransactionManager(); cacheManager.Setup(x => x.Get<ITransactionManager>(It.IsAny<string>())).Returns(transactionManager); }
public static void Main() { var sessionFactory = OWDatabaseConfiguration.Configure("thread_static"); var unitOfWorkFactory = new NHUnitOfWorkFactory(sessionFactory); using (var unitOfWork = unitOfWorkFactory.Create()) { var notionRepo = new NHRepository <NotionType>(sessionFactory); notionRepo.AddOrUpdate(new NotionType(Constants.NotionType)); notionRepo.AddOrUpdate(new NotionType(Constants.ActionType)); var relationRepo = new NHRepository <RelationType>(sessionFactory); relationRepo.AddOrUpdate(new RelationType(Constants.AssociationType)); relationRepo.AddOrUpdate(new RelationType(Constants.TaxonomyType)); relationRepo.AddOrUpdate(new RelationType(Constants.MeronomyType)); unitOfWork.Commit(); } }
/// <summary> /// Initialize current facility /// </summary> protected override void Init() { if (IsDebugEnabled) { log.Debug("Multiple UnitOfWork를 위한 Facility를 초기화 합니다."); } Kernel.Register(Component.For(typeof(INHRepository <>)).ImplementedBy(typeof(NHRepository <>))); var multipleUowFactory = new NHMultipleUnitOfWorkFactory(); if (_configs == null) { if (IsDebugEnabled) { log.Debug("프로그램에서 지정한 환경설정 정보가 없으므로, 환경설정 파일에서 정보를 얻습니다."); } AssertConfigurations(); _configs = FacilityConfig.Children.Select(factoryConfig => new NHUnitOfWorkFacilityConfig(factoryConfig)).ToArray(); multipleUowFactory.DefaultFactoryName = _defaultFactoryName; } if (IsDebugEnabled) { log.Debug("환경설정에 지정된 Factory 정보로부터, NHUnitOfWorkFactory를 생성합니다."); } foreach (NHUnitOfWorkFacilityConfig config in _configs) { var nestedUnitOfWorkFactory = new NHUnitOfWorkFactory(config.NHibernateConfigurationFilename); nestedUnitOfWorkFactory.RegisterSessionFactory(CreateSessionFactory(config)); multipleUowFactory.Add(nestedUnitOfWorkFactory); } Kernel.Register(Component.For <IUnitOfWorkFactory>().Instance(multipleUowFactory)); // Kernel.AddComponentInstance<IUnitOfWorkFactory>(multipleUowFactory); if (IsDebugEnabled) { log.Debug("Kernel에 NHMultipleUnitOfWorkFactory의 인스턴스를 등록했습니다."); } }
public virtual void SetUp() { Factory = Fluently.Configure() .Database(MsSqlConfiguration .MsSql2008 .ConnectionString(x => x.FromConnectionStringWithKey("testdb"))) .Mappings(mappings => mappings.FluentMappings.AddFromAssemblyOf <NHRepositoryTests>()) .ExposeConfiguration(config => { var exportMode = ConfigurationManager.AppSettings["schemaExportMode"]; switch (exportMode) { case ("Create"): new SchemaExport(config).Create(false, true); break; case ("Update"): new SchemaUpdate(config).Execute(false, true); break; case ("DropCreate"): new SchemaExport(config).Drop(false, true); new SchemaExport(config).Create(false, true); break; } }) .BuildSessionFactory(); Store.Local.Set("NHRepositoryTests.SessionFactory", Factory); NHUnitOfWorkFactory.SetSessionProvider( () => Store.Local.Get <ISessionFactory>("NHRepositoryTests.SessionFactory").OpenSession()); var locator = MockRepository.GenerateStub <IServiceLocator>(); locator.Stub(x => x.GetInstance <IUnitOfWorkFactory>()) .Return(new NHUnitOfWorkFactory()).Repeat.Any(); ServiceLocator.SetLocatorProvider(() => locator); HibernatingRhinos.NHibernate.Profiler.Appender.NHibernateProfiler.Initialize(); }
public void DropCreate() { SessionFactory = Fluently.Configure().Database( MsSqlConfiguration.MsSql2012.ConnectionString( @"Data Source=(localdb)\ProjectsV13;Initial Catalog=TestOW;Integrated Security=True;") .ShowSql) .CurrentSessionContext("thread_static") .Mappings(m => m.FluentMappings.AddFromAssemblyOf <NHUnitOfWork>()) .ExposeConfiguration( cfg => { // logging in output window of test cfg.SetInterceptor(new SqlStatementInterceptor()); var schema = new SchemaExport(cfg); schema.Drop(false, true); schema.Create(false, true); }) .BuildSessionFactory(); UnitOfWorkFactory = new NHUnitOfWorkFactory(SessionFactory); LinqProvider = new NHLinqProvider(SessionFactory); }
public virtual void TearDown() { NHUnitOfWorkFactory.SetSessionProvider(null); Store.Local.Clear(); HibernatingRhinos.NHibernate.Profiler.Appender.NHibernateProfiler.Stop(); }
public void Create_Throws_InvalidOperationException_When_No_SessionProvider_Has_Been_Set() { var factory = new NHUnitOfWorkFactory(); Assert.Throws <InvalidOperationException>(() => factory.Create()); }