// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Linq; using NHibernate; using NHibernate.Cfg; using NHibernate.Linq; namespace DBCache.DAL { public class DAL_NHib : IDAL { private ISessionFactory sf; public DAL_NHib(string cfgfnm, string mapfnm) { Configuration cfg = new Configuration(); cfg.Configure(cfgfnm); cfg.AddXmlFile(mapfnm); sf = cfg.BuildSessionFactory(); } public Big GetBig(int id) { Big res = null; using(ISession s = sf.OpenSession()) { res = s.QueryOver().Where(o => o.Id == id).Cacheable().SingleOrDefault(); } return res; } public int? GetCounter(String id) { int? res = null; using(ISession s = sf.OpenSession()) { Counter temp = s.QueryOver().Where(o => o.Id == id).Cacheable().SingleOrDefault(); if(temp != null) { res = temp.Val; } } return res; } public void IncrementCounter(String id) { using(ISession s = sf.OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { Counter temp = s.QueryOver().Where(o => o.Id == id).Cacheable().SingleOrDefault(); temp.Val = temp.Val + 1; s.Update(temp); tx.Commit(); } } } public void Close() { sf.Close(); } } }