public override void Load() { Kernel.Rebind<IDatabaseProvider>().ToMethod(context => { if (context.Kernel.Get<IEnsureDatabaseIsInstalled>().IsInstalled()) { var databaseSettings = context.Kernel.Get<DatabaseSettings>(); var typeByName = TypeHelper.GetTypeByName(databaseSettings.DatabaseProviderType); if (typeByName == null) return null; return context.Kernel.Get(typeByName) as IDatabaseProvider; } return null; }); _configurator = _configurator ?? new NHibernateConfigurator(Kernel.Get<IDatabaseProvider>()); _configurator.CacheEnabled = _cacheEnabled; Kernel.Bind<ISessionFactory>() .ToMethod( context => _configurator.CreateSessionFactory()) .InSingletonScope(); if (_forWebsite) { Kernel.Bind<ISession>().ToMethod(GetSession).InRequestScope(); } else { Kernel.Bind<ISession>().ToMethod(GetSession).InThreadScope(); } Kernel.Bind<IStatelessSession>() .ToMethod(context => context.Kernel.Get<ISessionFactory>().OpenStatelessSession()).InRequestScope(); }
private void SetUpInitialData(InstallModel model, IDatabaseProvider provider) { var configurator = new NHibernateConfigurator(provider); ISessionFactory sessionFactory = configurator.CreateSessionFactory(); ISession session = sessionFactory.OpenFilteredSession(CurrentRequestData.CurrentContext); IStatelessSession statelessSession = sessionFactory.OpenStatelessSession(); var kernel = MrCMSApplication.Get<IKernel>(); kernel.Rebind<ISession>().ToMethod(context => session); kernel.Rebind<IStatelessSession>().ToMethod(context => statelessSession); var site = new Site { Name = model.SiteName, BaseUrl = model.SiteUrl, CreatedOn = DateTime.UtcNow, UpdatedOn = DateTime.UtcNow }; using (ITransaction transaction = statelessSession.BeginTransaction()) { statelessSession.Insert(site); transaction.Commit(); } CurrentRequestData.CurrentSite = site; kernel.Get<IInitializeDatabase>().Initialize(model); kernel.Get<ICreateInitialUser>().Create(model); kernel.GetAll<IOnInstallation>() .OrderBy(installation => installation.Priority) .ForEach(installation => installation.Install(model)); }