Skip to content
This repository was archived by the owner on Jun 30, 2021. It is now read-only.

Simplify.FluentNHibernate

Alexanderius edited this page Aug 26, 2014 · 11 revisions

Provides:

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

Configuration example

Session factory configuration and creation

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

                public ISessionFactory Instance
                {
                        get { return _instance; }
                }

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

App.config or Web.config data-base configuration

<?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>

Session lambda extensions example

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

More examples here

Clone this wiki locally