Loquacious configuration

Loquacious configuration is the latest addition to the family of configuration options that NHibernate comes bundled with and it is the best option so far. Loquacious offers a code based and easy to infer API for configuring NHibernate. Parts of the API are also fluent which makes it less verbose to work with. Loquacious API is built by extending the programmatic configuration API and hence can be called a part of programmatic configuration. Without much ado, let's jump straight into the code:

public class LoquaciousConfiguration
{
  private readonly ISession session;

  public LoquaciousConfiguration()
  {
    var config = new Configuration();
    config.DataBaseIntegration(db =>
    {
      db.Dialect<SQLiteDialect>();
      db.Driver<SQLite20Driver>();
      db.ConnectionString = "data source=:memory:";
      db.ConnectionReleaseMode =
      ConnectionReleaseMode.OnClose;
      db.LogSqlInConsole = true;
      db.LogFormattedSql = true;
    })
    .AddMapping(GetMappings());

    var sessionFactory = config.BuildSessionFactory();
    session = sessionFactory.OpenSession();

    using (var tx = session.BeginTransaction())
    {
      new SchemaExport(config).Execute(
      new SchemaExport(config).Execute(
                                           useStdOut:true, 
                                           execute:true,
                                           justDrop:false, 
                                           connection:session.Connection,
                                           exportOutput:Console.Out);
                                           tx.Commit();
    }
    session.Clear();
  }

  private HbmMapping GetMappings()
  {
    var mapper = new ModelMapper();
    mapper.AddMapping<EmployeeMappings>();
    mapper.AddMapping<AddressMappings>();
    mapper.AddMapping<BenefitMappings>();
    mapper.AddMapping<LeaveMappings>();
    mapper.AddMapping<SkillsEnhancementAllowanceMappings>();
    mapper.AddMapping<SeasonTicketLoanMappings>();
    mapper.AddMapping<CommunityMappings>();
    return
    mapper.CompileMappingForAllExplicitlyAddedEntities();
  }

  public ISession Session
  {
    get { return session; }
  }
}

Similar to programmatic configuration, we first create an instance of the Configuration class. After that point, we use extension methods named DatabaseIntegration to configure various properties including database dialect, driver, connection string, and so on. You can notice that configuration of dialect and driver has been made easy by making their types as generic parameters to each methods. This works really nicely if you are using tools such as ReSharper. In the same way, other properties such as connection.release_mode, show_sql, format_sql, and so on, are wrapped inside easy to use and typed properties.

The part where we add mappings is almost same as programmatic configuration. We have moved the ModelMapper code into a separate method GetMappings in order to make the code more readable. Otherwise, it is exactly the same code we have used earlier with programmatic configuration. Rest of the part to build session factory and use of SchemaExport to build the database schema into SQLite in-memory database is exactly same as the earlier examples.

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

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