In this recipe, I'll show you how we can set up the data access layer to wrap all data access in NHibernate transactions automatically.
Complete the Eg.Core
model and mappings from Chapter 1.
Create a new class library named
Eg.Core.Data
.Add a reference to
NHibernate.dll
and theEg.Core
project.Add the following two DAO classes:
public class DataAccessObject<T, TId> where T : Entity<TId> { private readonly ISessionFactory _sessionFactory; private ISession session { get { return _sessionFactory.GetCurrentSession(); } } public DataAccessObject(ISessionFactory sessionFactory) { _sessionFactory = sessionFactory; } public T Get(TId id) { return Transact(() => session.Get<T>(id)); } public T Load(TId id) { return Transact(() => session.Load<T>(id)); } public void Save(T entity) { Transact(() => session.SaveOrUpdate...