I used in my project NHibernate like this:
public class DBHelper
{
private static ISessionFactory sessions;
public static void Configure()
{
sessions = new Configuration().Configure().AddClass(typeof(Clients)).BuildSessionFactory();
//ISessionFactory factory = Fluently.Configure().Mappings(m => m.FluentMappings.AddFromAssemblyOf<Clients>()).Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey(DBConnection.GetConnectionString())).BuildSessionFactory();
}
public static void Insert(Clients pb)
{
using (ISession session = sessions.OpenSession())
using (ITransaction tx = session.BeginTransaction())
{
session.Save(pb);
tx.Commit();
}
}
public static void UpdateContact(Clients pb)
{
using (ISession session = sessions.OpenSession())
using (ITransaction tx = session.BeginTransaction())
{
session.Update(pb);
tx.Commit();
}
}
public static void DeleteContact(Clients pb)
{
using (ISession session = sessions.OpenSession())
using (ITransaction tx = session.BeginTransaction())
{
session.Delete(pb);
tx.Commit();
}
}
}
I have tree simple methods: insert, delete, save.
Now I need to get all data from DB table or some data like analog
select * from Clients where id=’…’
Your options are:
Criteria Queries
QueryOver Queries
HQL
Linq
Native SQL
Personally, I really like the QueryOver API:
If you only need to get an entity by its ID, you can use Get or Load
If you search for NHibernate tutorials you’ll certainly find a lot of information. I’ve heard good things about the Summer Of NHibernate screencasts, though I personally haven’t seen them.