Fluent configuration

If you are using FNH for declaring mappings, then loquacious configuration is not an option. This is mainly because loquacious configuration would not be able to recognize fluent mappings. Besides that, FNH offers its own fluent API to configure session factory. The fluent API is more or less similar to loquacious API in terms of functionality offered. But fluent API tries to be less verbose as compared to loquacious. Let's do the same configuration we did using loquacious configuration, but using fluent API this time:

var fluentConfig = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory()
                             .ShowSql().FormatSql())
.Mappings(mapper =>
{
  mapper.FluentMappings.Add<EmployeeMappings>();
  mapper.FluentMappings
  .Add<CommunityMappings>();
  mapper.FluentMappings.Add<BenefitMappings>();
  mapper.FluentMappings.Add<LeaveMappings>();
  mapper.FluentMappings.Add<SkillsEnhancementAllowanceMappings>();
  mapper.FluentMappings.Add<SeasonTicketLoanMappings>();
  mapper.FluentMappings.Add<AddressMappings>();
});

var nhConfiguration = fluentConfig.BuildConfiguration();
var sessionFactory = nhConfiguration.BuildSessionFactory();
session = sessionFactory.OpenSession();
using (var tx = session.BeginTransaction())
{
  new SchemaExport(nhConfiguration)
  .Execute(useStdOut:true,
           execute:true,
           justDrop:false,
           connection:session.Connection,
           exportOutput:Console.Out);
  tx.Commit();
}

Let's take a look at important lines from the preceding code snippet:

  • At the root of the fluent configuration API is a static class named Fluently. Calling a method named Configure() on this class gives us an instance of the FluentConfiguration class. This class exposes methods that you can use to configure NH.
  • First method we call is Database. Here, you specify dialect using a set of helper classes that FNH has provided. In the preceding example, we have used helper class named SQLiteConfiguration. This class has a property named Standard for usual declaration of SQLite dialect. In the Dialect section ahead, we will look at other available helper classes in detail.
  • Along with database configuration, we also enabled the show_sql and format_sql options by calling the ShowSql and FormatSql methods respective. Note how we can easily just chain the methods.
  • We then called the Mappings method to add fluent mappings to configuration. Here, we added individual mappings explicitly. In the Mappings section ahead, we will look at this in detail with other options of adding mappings.
  • At this point the fluent configuration object is ready to be used. If we need to access the underlying NHibernate configuration object then we can call the BuildConfiguration method on the fluent configuration object as we have done previously. We need the NHibernate configuration object to use with the SchemaExport class. If there is no need for the underlying NHibernate configuration, then you can call the BuildSessionFactory method directly on the fluent configuration object.

    Note

    By calling BuildConfiguration, we get access to NHibernate configuration object after it is built, but if we need to access it before it is built then a method named ExposeConfiguration is available. This method is useful if you want to set some property on configuration that is not directly supported by Fluent API. Following short example shows how to use this method to set command_timeout value:

    var fluentConfig = Fluently.Configure()
    .Database(SQLiteConfiguration.Standard.InMemory()
    .ExposeConfiguration(cfg =>
    {
      cfg.SetProperty(Environment.CommandTimeout, "30");
    });
    

That is it really about the preceding code. Rest of the code to deal with building session factory, opening session, and generating database creation scripts is no different than what we have seen earlier.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.186.143