Skip to content

Simplify.FluentNHibernate

Alexanderius edited this page Oct 29, 2019 · 2 revisions

Simplify.FluentNHibernate Documentation

Provides:

  • FluentConfiguration class extensions which allows you to easily configure your data-base connection.
  • NHibernate ISession extensions which allows you to write queries with lambda expressions.
  • SchemaExporter

Available at NuGet as binary package

Quick Start Example

Configuration through Microsoft.Extensions.Configuration

Session factory setup

namespace MyApp.Database
{
        public class MyDbSessionFactoryBuilder
        {
                private readonly ISessionFactory _instance;

                public ISessionFactory Instance
                {
                        get { return _instance; }
                }

                public MyDbSessionFactoryBuilder(IConfiguration cfg, string configSectionName = "MyDatabaseConnectionSettings")
                {
                        var configuration = Fluently.Configure();
                        configuration.InitializeFromConfigMsSql(cfg, configSectionName);
                        configuration.AddMappingsFromAssemblyOf<MyDbSessionFactoryBuilder>();
                        _instance = configuration.BuildSessionFactory();
                }
        }
}

IConfiguration settings example based on appsettings.json

{
    "MyDatabaseConnectionSettings":
    {
        "ServerName": "Server name",
        "DataBaseName": "data-base name",
        "UserName": "user name",
        "UserPassword": "password",
    }
}

Configuration through *.config files

Session factory setup

namespace MyApp.Database
{
        public class MyDbSessionFactoryBuilder
        {
                private readonly ISessionFactory _instance;

                public ISessionFactory Instance
                {
                        get { return _instance; }
                }

                public MyDbSessionFactoryBuilder(string configSectionName = "MyDatabaseConnectionSettings")
                {
                        var configuration = Fluently.Configure();
                        configuration.InitializeFromConfigMsSql(configSectionName);
                        configuration.AddMappingsFromAssemblyOf<MyDbSessionFactoryBuilder>();
                        _instance = configuration.BuildSessionFactory();
                }
        }
}

Configuration file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
        <configSections>
                <section name="MyDatabaseConnectionSettings" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </configSections>

        <MyDatabaseConnectionSettings>
                <add key="ServerName" value="Server name" />
                <add key="DataBaseName" value="data-base name" />

                <add key="UserName" value="user name" />
                <add key="UserPassword" value="password" />
        </MyDatabaseConnectionSettings>
</configuration>

FluentNHibernate quick setup variants

Database type Method Comment
Microsoft SQL Server InitializeFromConfigMsSql Connection using System.Data managed driver
MySQL InitializeFromConfigMySql Connection using MySql.Data managed driver
PostgreSQL InitializeFromConfigPostgreSql Connection using Npgsql managed driver
Oracle InitializeFromConfigOracleOdpNet Connection using Oracle.ManagedDataAccess managed driver (recommended for Oracle)
Oracle InitializeFromConfigOracleOdpNetNative Connection using odp.net.x86 or odp.net.x64 unmanaged drivers
Oracle InitializeFromConfigOracleClient Connection using Oracle.DataAccess.x86 or Oracle.DataAccess.x64 unmanaged drivers
SQLite InitializeFromConfigSqLite Connection using System.Data.SQLite managed driver
SQLite InitializeFromConfigSqLiteInMemory Connection using System.Data.SQLite managed driver using in-memory database

Additional configuration options

Option Name Type Description
Port int Database port number
ShowSql bool Executed SQL queries can be displayed in console/(VS output window) by setting ShowSql option, default values is false

Custom NHibernate configuration

Can be specified via lambda extension, for example, a custom dialect

configuration.InitializeFromConfigMsSql(configSectionName, c => c.Dialect<MsSql2012Dialect>());

Session lambda extensions for data retrieval

Example

var user = session.GetSingleObject(x => x.Name == "FooName");
var users = session.GetList(x => x.Name.StartWith("A"));

More examples here

You can’t perform that action at this time.