Last active
July 31, 2017 23:26
-
-
Save jarrettmeyer/4685025 to your computer and use it in GitHub Desktop.
How I configure NHibernate with Ninject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class NHibernateSessionManager | |
| { | |
| private const string CONNECTION_STRING_NAME = "..."; | |
| private static Configuration configuration; | |
| private static readonly ILog log; | |
| private static ISessionFactory sessionFactory; | |
| static NHibernateSessionManager() | |
| { | |
| log = LogManager.GetLogger(typeof(NHibernateSessionManager)); | |
| ConnectionString = ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString; | |
| } | |
| public static Configuration Configuration | |
| { | |
| get | |
| { | |
| EnsureSessionFactoryHasBeenInitialized(); | |
| return configuration; | |
| } | |
| } | |
| public static string ConnectionString { get; private set; } | |
| public static ISession CurrentSession | |
| { | |
| get | |
| { | |
| EnsureSessionFactoryHasBeenInitialized(); | |
| if (HasCurrentlyBoundSession) | |
| return sessionFactory.GetCurrentSession(); | |
| var session = sessionFactory.OpenSession(); | |
| CurrentSessionContext.Bind(session); | |
| return session; | |
| } | |
| } | |
| public static IStatelessSession CurrentStatelessSession | |
| { | |
| get | |
| { | |
| EnsureSessionFactoryHasBeenInitialized(); | |
| return sessionFactory.OpenStatelessSession(); | |
| } | |
| } | |
| public static ISessionFactory SessionFactory | |
| { | |
| get | |
| { | |
| EnsureSessionFactoryHasBeenInitialized(); | |
| return sessionFactory; | |
| } | |
| } | |
| public static void CloseSession() | |
| { | |
| if (sessionFactory == null) | |
| return; | |
| if (HasCurrentlyBoundSession) | |
| CloseAndUnbindSessionInstance(); | |
| } | |
| private static void CloseAndUnbindSessionInstance() | |
| { | |
| var session = CurrentSessionContext.Unbind(sessionFactory); | |
| session.Close(); | |
| log.Debug("session closed"); | |
| } | |
| /// <summary> | |
| /// Resets the NHibernate session factory. | |
| /// <para>This method is provided to simplify testing.</para> | |
| /// </summary> | |
| public static void ResetSessionFactory() | |
| { | |
| sessionFactory = null; | |
| } | |
| private static bool HasCurrentlyBoundSession | |
| { | |
| get { return CurrentSessionContext.HasBind(sessionFactory); } | |
| } | |
| private static HbmMapping Mapping | |
| { | |
| get | |
| { | |
| var mapping = ModelMapper.CompileMappingForAllExplicitlyAddedEntities(); | |
| return mapping; | |
| } | |
| } | |
| private static ModelMapper ModelMapper | |
| { | |
| get | |
| { | |
| Assembly mappingAssembly = ...; // Put your assembly containing your mappings here. | |
| var modelMapper = new ModelMapper(); | |
| List<Type> exportedTypes = new List<Type>(); | |
| exportedTypes.AddRange(mappingAssembly.GetExportedTypes()); | |
| modelMapper.AddMappings(exportedTypes); | |
| return modelMapper; | |
| } | |
| } | |
| private static void EnsureSessionFactoryHasBeenInitialized() | |
| { | |
| if (sessionFactory == null) | |
| InitializeSessionFactory(); | |
| } | |
| private static void ConfigureBatchSize() | |
| { | |
| configuration.SetProperty("adonet.batch_size", "1"); | |
| } | |
| private static void ConfigureDatabase() | |
| { | |
| configuration.DataBaseIntegration(db => | |
| { | |
| db.ConnectionString = ConnectionString; | |
| db.Dialect<MsSql2008Dialect>(); | |
| db.Driver<SqlClientDriver>(); | |
| db.BatchSize = 50; | |
| db.Timeout = 20; | |
| // enabled for testing | |
| db.AutoCommentSql = true; | |
| }); | |
| } | |
| private static void ConfigureEventListeners() | |
| { | |
| // If you have any event listeners, put them here. | |
| new AuditEventListener().Register(configuration); | |
| new CustomSaveEventListener().Register(configuration); | |
| } | |
| private static void ConfigureMappings() | |
| { | |
| configuration.AddMapping(Mapping); | |
| } | |
| private static void ConfigureSessionContext<T>() where T : ICurrentSessionContext | |
| { | |
| configuration.CurrentSessionContext<T>(); | |
| } | |
| private static void ConfigureStatistics() | |
| { | |
| configuration.SessionFactory().GenerateStatistics(); | |
| } | |
| private static void InitializeSessionFactory() | |
| { | |
| if (HttpContext.Current != null) | |
| InitializeWebSessionContextSessionFactory(); | |
| else | |
| InitializeThreadStaticSessionContextSessionFactory(); | |
| } | |
| private static void InitializeThreadStaticSessionContextSessionFactory() | |
| { | |
| sessionFactory = GetSessionFactory<ThreadStaticSessionContext>(); | |
| log.Debug("Created new NHibernate session factory with ThreadStaticSessionContext."); | |
| } | |
| private static void InitializeWebSessionContextSessionFactory() | |
| { | |
| sessionFactory = GetSessionFactory<WebSessionContext>(); | |
| log.Debug("Created new NHibernate session factory with WebSessionContext."); | |
| } | |
| private static ISessionFactory GetSessionFactory<T>() where T : ICurrentSessionContext | |
| { | |
| configuration = new Configuration(); | |
| ConfigureDatabase(); | |
| ConfigureSessionContext<T>(); | |
| ConfigureStatistics(); | |
| ConfigureMappings(); | |
| ConfigureEventListeners(); | |
| ConfigureBatchSize(); | |
| sessionFactory = configuration.BuildSessionFactory(); | |
| return sessionFactory; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This part might be confusing. | |
| // I am binding Ninject InTransientScope, which means that nothing will be stored in | |
| // Ninject's activation cache. My NHibernate configuration is smart enough to manage | |
| // its own session lifecycle using CurrentSessionContext. | |
| Kernel.Bind<ISession>().ToMethod(_ => NHibernateSessionManager.CurrentSession).InTransientScope(); | |
| Kernel.Bind<IStatelessSession>().ToMethod(_ => NHibernateSessionManager.CurrentStatelessSession).InTransientScope(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The NinjectDatabaseConfiguration.cs file has just these lines? Where do I store this? There's no kind of "run()"?