Utility class for NHibernate
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
NHUtil
.gitignore
README.md

README.md

NHibernateUtil

This C# utility class makes the NHibernate + Fluent NHibernate more easier and fun!

This simple class comes pre configured for MySQL, PostgreSQL, SQL Server 2008, SQLite and Oracle 10g.

You can connect/disconnect and execute the basic CRUD (with batch support).

Requirements

  • Nhiberante 3 and later
  • Fluent NHibernate 1.2 and later
  • Database drivers for .NET platform that you will use
    • MySQL
    • PostgreSQL
    • SQL Server 2008
    • SQLite
    • Oracle 10g

How to use

Let's use this POCO and this Fluent Map for the examples:

public class Person
{
    public virtual int id { get; set; }
    public virtual string name { get; set; }
    public virtual int age{ get; set; }
}

public class MapPerson : ClassMap<Person>
{
    public MapPerson()
    {
        Table("people");
        Id(x => x.id);
        Map(x => x.name);
        Map(x => x.age);
    }
}

First of all we need to connect to the database. Below are possible ways to connect with the DBMS.

// MySQL
NHUtil.Connect(DBMS.MySQL, "localhost", 3306, "test_nh", "root", "root");

// PostgreSQL
NHUtil.Connect(DBMS.PostgreSQL, "localhost", 5432, "test_nh", "postgres", "root");

// SQLServer 2008
NHUtil.Connect(DBMS.SQLServer2008, @".\SQLEXPRESS", 0, "test_nh", null, null);

// SQLite
NHUtil.Connect(DBMS.SQLite, null, 0, @"c:\test_nh.db", null, null);

// Oracle
// Not yet been tested

To disconnect:

NHUtil.Disconnect();

ATTENTION: In most of cases you only need to connect and disconnect from database ONE TIME while your application is running.

CRUD

For SELECT all records:

Person[] people = NHUtil.Select<Person>();
// or
List<Person> people = new List<Person>(NHUtil.Select<Person>());

For SELECT just one record by ID:

Person person = NHUtil.Select<Person>(1);

The INSERT:

Person person = new Person();
person.name = "Richard";
person.age = 23;
NHUtil.SaveOrUpdate(person);

The UPDATE:

Person person = NHUtil.Select<Person>(1);
person.name = "Tom";
person.age = 34;
NHUtil.SaveOrUpdate(person);

The DELETE:

Person person = NHUtil.Select<Person>(1);
NHUtil.Delete(person);

Batch INSERT, UPDATE and DELETE

The only thing you need to do is send a object[] this way:

// INSERT
List<Person> people = new List<Person>();
for (int i = 0; i < 10; i++)
{
    Person person = new Person();
    person.name = "Person " + (i + 10);
    person.age = new Random().Next(1, 99);
    
    people.Add(person);
}

NHUtil.SaveOrUpdate(people.ToArray());

// DELETE
Person[] people = NHUtil.Select<Person>();
NHUtil.Delete(people);

Simple like that!