/// <summary> /// Retursn the NHibernate session to use /// </summary> /// <returns></returns> internal ISession GetNHibernateSession(bool isReadonly, bool forceNewUnbounded = false) { //if a session has been supplied via the constructor (i.e. for testing) if (SingleProvidedSession != null) { if (!IsSingleSessionFinalized) { using (new WriteLockDisposable(Locker)) { IsSingleSessionFinalized = true; //add the session to the finalizer FrameworkContext.ScopedFinalizer.AddFinalizerToScope(SingleProvidedSession, x => UnbindAndCloseSession(NhSessionFactory)); } } EnsureFlushMode(SingleProvidedSession as ISessionImplementor, isReadonly); return(CheckFirstRunTasks(SingleProvidedSession)); } // See http://nhforge.org/doc/nh/en/index.html#architecture-current-session // for details on contextual sessions var sessionFactory = GetNHibernateSessionFactory(); if (!forceNewUnbounded) { //if we have a session factory, then check if its bound if (sessionFactory != null && CurrentSessionContext.HasBind(sessionFactory)) { LogHelper.TraceIfEnabled <EntityRepositoryFactory>("GetNHibernateSession: using ISessionFactory.GetCurrentSession()"); var boundSession = NhSessionFactory.GetCurrentSession(); EnsureFlushMode(boundSession as ISessionImplementor, isReadonly); return(CheckFirstRunTasks(boundSession)); } } LogHelper.TraceIfEnabled <EntityRepositoryFactory>("GetNHibernateSession: using ISessionFactory.OpenSession()"); var sessionToReturn = NhSessionFactory.OpenSession(); EnsureFlushMode(sessionToReturn as ISessionImplementor, isReadonly); //add the new session to the finalizer FrameworkContext.ScopedFinalizer.AddFinalizerToScope(sessionToReturn, x => UnbindAndCloseSession(NhSessionFactory)); if (!forceNewUnbounded) { //dont bind the session if we're forcing new for nested transactions CurrentSessionContext.Bind(sessionToReturn); } return(CheckFirstRunTasks(sessionToReturn)); }
protected override void DisposeResources() { if (NhSessionFactory == null) { return; } UnbindAndCloseSession(NhSessionFactory); NhSessionFactory.Dispose(); }
protected override void Load(ContainerBuilder builder) { string host = "192.168.99.100"; int port = 5432; string database = "postgres"; string userName = "******"; string password = "******"; builder.RegisterType <TransactionInterceptor>().SingleInstance(); builder.RegisterInstance(NhSessionFactory.Create(host, port, database, userName, password)) .As <ISessionFactory>() .SingleInstance(); builder.RegisterType <NhUnitOfWork>().As <IUnitOfWork>(); }
public static Configuration UseRepositories(this Configuration configuration, string connectionSection, params Assembly[] assemblies) { configuration.RegisterRepositoriesAssembly(assemblies); string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionSection].ConnectionString; string providerName = System.Configuration.ConfigurationManager.ConnectionStrings[connectionSection].ProviderName; IPersistenceConfigurer configurer = null; switch (providerName) { case "System.Data.SqlClient": configurer = MsSqlConfiguration.MsSql2008.ConnectionString(connectionString) .AdoNetBatchSize(500) .Dialect <NHibernate.Dialect.MsSql2008Dialect>() .FormatSql() .ShowSql() .UseOuterJoin(); break; case "MySql.Data.MySqlClient": configurer = MySQLConfiguration.Standard.ConnectionString(connectionString) .AdoNetBatchSize(500) .Dialect <NHibernate.Dialect.MySQL55InnoDBDialect>() .FormatSql() .ShowSql() .UseOuterJoin(); break; } NhSessionFactory.Instance(configurer, configuration.repositoryAssemblies.ToArray()); Engine.RegisterDataBase(providerName, connectionString); foreach (Assembly assembly in configuration.repositoryAssemblies) { Type[] types = assembly.GetTypes().Where(item => typeof(IRepository).IsAssignableFrom(item) && !item.IsAbstract).ToArray(); ((AutofacObjectContainer)ObjectContainer.Current).ContainerBuilder.RegisterTypes(types).AsImplementedInterfaces().SingleInstance(); } return(configuration); }
public RoomBookingController() { _session = NhSessionFactory.OpenSession(); _hotelReservationService = new HotelReservationService(_session); }
public SettingController() { _session = NhSessionFactory.OpenSession(); _userService = new UserService(_session); _menuService = new MenuService(_session); }
public AccountController() { _session = NhSessionFactory.OpenSession(); _userService = new UserService(_session); }