msgbartop
Around Software Development and Innovation
msgbarbottom

27 Feb 11 Repository pattern & Nhibernate

Ho scritto tempo fa un software che doveva girare su ASP.NET basato su DDD. L’ORM usato รจ NHibernate. Ho usato un semplice repository di cui riporto il sorgente (viene usato Windsor anche per fare IOC).:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jobbing.DAL.Repository
{

//Repository Interface
    public interface IRepository<T>
    {
        void Save(T obj);
        void Update(T obj);
        void Delete(T obj);
        T Load<T>(object id);
        T GetReference<T>(object id);
        void DeleteAll(IList<T> objs);
        void UpdateAll(IList<T> objs);
        void InsertAll(IList<T> objs);
        IList<T> GetAll<T>();
        IList<T> GetAllOrdered<T>(string propertyName,bool Ascending);
        IList<T> Find<T>(IList<string> criteria);
        void Detach(T item);
        IList<T> GetAll<T>(int pageIndex, int pageSize);
        void Commit();
        void Rollback();
        void BeginTransaction();
    }
}

//Implementation of NHibernate Repository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using NHibernate;
using NHibernate.Expression;
using NHibernate.Engine.Query;
using NHibernate.Mapping;

namespace Jobbing.DAL.Repository
{
    public class NHibernateRepository<T> : IRepository<T>
    {
        private ISession session;

        public NHibernateRepository()
        {
            session = NHibernateSessionManager.Instance.GetSession();
        }

        #region IRepository<T> Members

        public void Save(T obj)
        {
            session.Save(obj);
        }

        public void Update(T obj)
        {
            session.Update(obj);
        }

        public void Delete(T obj)
        {
            session.Delete(obj);
        }

        public T Load<T>(object id)
        {
            return session.Load<T>(id);
        }

        public T GetReference<T>(object id)
        {
            return session.Get<T>(id);
        }

        public void DeleteAll(IList<T> objs)
        {
            for (Int32 I = 0; I < objs.Count; ++I)
            {
                Delete(objs[I]);
            }
        }

        public void UpdateAll(IList<T> objs)
        {
            for (Int32 I = 0; I < objs.Count; ++I)
            {
                Update(objs[I]);
            }
        }

        public void InsertAll(IList<T> objs)
        {
            for (Int32 I = 0; I < objs.Count; ++I)
            {
                Save(objs[I]);
            }
        }

        public void Detach(T item)
        {
            session.Evict(item);
        }

        internal void Flush()
        {
            session.Flush();
        }

        public IList<T> GetAll<T>(int pageIndex, int pageSize)
        {
            ICriteria criteria = session.CreateCriteria(typeof(T));
            criteria.SetFirstResult(pageIndex * pageSize);
            if (pageSize > 0)
            {
                criteria.SetMaxResults(pageSize);
            }
            return criteria.List<T>();
        }

        public IList<T> GetAll<T>()
        {
            return GetAll<T>(0, 0);
        }

        public IList<T> Find<T>(IList<string> strs)
        {
            System.Collections.Generic.IList<ICriterion> objs = new System.Collections.Generic.List<ICriterion>();
            foreach(string s in strs){
                ICriterion cr1 = Expression.Sql(s);
                objs.Add(cr1);
            }
            ICriteria criteria = session.CreateCriteria(typeof(T));
            foreach (ICriterion rest in objs)
                session.CreateCriteria(typeof(T)).Add(rest);

            criteria.SetFirstResult(0);
            return criteria.List<T>();
        }

        public void Commit()
        {
            if (session.Transaction.IsActive)
            {
                session.Transaction.Commit();
            }
        }

        public void Rollback()
        {
            if (session.Transaction.IsActive)
            {
                session.Transaction.Rollback();
                session.Clear();
            }
        }

        public void BeginTransaction()
        {
            session.BeginTransaction();
        }

        #endregion

        #region IRepository<T> Members

        public IList<T> GetAllOrdered<T>(string propertyName, bool Ascending)
        {
            Order cr1 = new Order(propertyName, Ascending);
            IList<T> objsResult = session.CreateCriteria(typeof(T)).AddOrder(cr1).List<T>();
            return objsResult;

        }

        #endregion
    }
}
Ho usato questo codice unito al pattern Open Session in View ;)
Bye
Antonio

Leave a Comment