Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7997225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:55:25+00:00 2026-06-04T14:55:25+00:00

I have implement testing app. which uses fluent nhibernate mapping to db object inside

  • 0

I have implement testing app. which uses fluent nhibernate mapping to db object inside mssql db. Since I want to learn fine tune nhib. mvc3 applications, I’m using this app. for testing purposes which have only one simple entity with 10 enum properties and one string property.
So, it is really lightwave, yet startup time according to nhibernate profiler is 4.37 sec. Which is really slow for rendering one entity with few lines checked/unchecked property.

Code is the following.
Domain.SessionProvider.cs

public static ISessionFactory CreateSessionFactory()
{
   var config = Fluently.Configure()
          .Database(MsSqlConfiguration.MsSql2008
          .ConnectionString(c => c.FromConnectionStringWithKey("myConnection")))
          .Mappings(m => m.FluentMappings.Add<FeaturesMap>())
          .ExposeConfiguration(p => p.SetProperty("current_session_context_class", "web"))
          .BuildConfiguration();

          return config.BuildSessionFactory();            
}

Global.asax

public class MvcApplication : System.Web.HttpApplication
{   
   //SessionPerWebRequest is ommited here as well as other content
   public static ISessionFactory SessionFactory =
               SessionProvider.CreateSessionFactory();

    protected void Application_Start()
    {
       SessionFactory.OpenSession();
    }
}

Inside myController I have following:

public ActionResult Index()
{
   return View(GetData());
}

private IList<FeaturesViewModel> GetData()
{
     List<Features> data;
     using (ISession session = MvcApplication.SessionFactory.GetCurrentSession())
     {
          using (ITransaction tx = session.BeginTransaction())
          {
              data = session.Query<Features>().Take(5).ToList();
              tx.Commit();

              var viewModelData = FeaturesViewModel.FromDomainModel(data);
              return viewModelData;
           }
      }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:55:27+00:00Added an answer on June 4, 2026 at 2:55 pm

    You can improve the startup time (of both web and windows applications) by caching the Configurations. The following class will do this job:

    using System.IO;
    using System.Reflection;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Web;
    using NHibernate.Cfg;
    
    namespace NH32AutoMap.Core
    {
        public class ConfigurationFileCache
        {
            private readonly string _cacheFile;
            private readonly Assembly _definitionsAssembly;
    
            public ConfigurationFileCache(Assembly definitionsAssembly)
            {
                _definitionsAssembly = definitionsAssembly;
                _cacheFile = "nh.cfg";
                if (HttpContext.Current != null) //for the web apps
                    _cacheFile = HttpContext.Current.Server.MapPath(
                                    string.Format("~/App_Data/{0}", _cacheFile)
                                    );
            }
    
            public void DeleteCacheFile()
            {
                if (File.Exists(_cacheFile))
                    File.Delete(_cacheFile);
            }
    
            public bool IsConfigurationFileValid
            {
                get
                {
                    if (!File.Exists(_cacheFile))
                        return false;
                    var configInfo = new FileInfo(_cacheFile);
                    var asmInfo = new FileInfo(_definitionsAssembly.Location);
    
                    if (configInfo.Length < 5 * 1024)
                        return false;
    
                    return configInfo.LastWriteTime >= asmInfo.LastWriteTime;
                }
            }
    
            public void SaveConfigurationToFile(Configuration configuration)
            {
                using (var file = File.Open(_cacheFile, FileMode.Create))
                {
                    var bf = new BinaryFormatter();
                    bf.Serialize(file, configuration);
                }
            }
    
            public Configuration LoadConfigurationFromFile()
            {
                if (!IsConfigurationFileValid)
                    return null;
    
                using (var file = File.Open(_cacheFile, FileMode.Open, FileAccess.Read))
                {
                    var bf = new BinaryFormatter();
                    return bf.Deserialize(file) as Configuration;
                }
            }
        }
    }
    

    To use that,

    private Configuration readConfigFromCacheFileOrBuildIt()
    {
        Configuration nhConfigurationCache;
        var nhCfgCache = new ConfigurationFileCache(MappingsAssembly);
        var cachedCfg = nhCfgCache.LoadConfigurationFromFile();
        if (cachedCfg == null)
        {
            nhConfigurationCache = buildConfiguration();
            nhCfgCache.SaveConfigurationToFile(nhConfigurationCache);
        }
        else
        {
            nhConfigurationCache = cachedCfg;
        }
        return nhConfigurationCache;
    }
    

    And then before calling the BuildSessionFactory, we can read the config file from cache or if the mappings have changed, build it and cache it again:

    public ISessionFactory SetUpSessionFactory()
    {
        var config = readConfigFromCacheFileOrBuildIt();
        var sessionFactory = config.BuildSessionFactory();
    

    Here you can find a full sample: (^).
    + If you want to make it work, separate domain classes and mappings definitions assemblies from the main application’s assembly (because the ConfigurationFileCache class will delete the cache file if the mappings definitions assembly is newer than the cache file’s LastWriteTime).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails app (Rails - 2.3.8, Linux) which uses a MySQL database,
I have been using ASP.NET MVC 3 for a while to implement a testing
I have implement FragmentPagerAdapter in my app but it show only a same list
I have implement a scenario which involves two way communication between child and parent
I have to implement something in an Excel macro (yuk!), for which I would
I have to implement logic where I need to atomically set an object for
I have a web-application which uses hibernate and for some reason every thread (httprequest
I have a winforms app that uses a strongly typed custom DataSet for holding
I have implemented sharekit in the app I am making. In my final testing,
I have a simple wpf app which has a button that increments a value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.