Click here to Skip to main content
15,896,154 members
Articles / Web Development / Apache

The platform-independent code with Mono: Client-server application sample

Rate me:
Please Sign up or sign in to vote.
4.80/5 (23 votes)
24 Mar 2010CPOL10 min read 75K   798   59  
This article shows how we can develop the platform-independent software with Mono usage
using System;
using System.Collections.Generic;
using System.Text;

using NHibernate;
//using NHibernate.Linq;

using NHibernate.Criterion;

namespace InfoCenter.Persistence.Core
{
    public class Repository<T> : IRepository<T>
    {
        private ISessionFactory _sessionFactory;
        private ISession Session
        {
            get
            {
                return _sessionFactory.OpenSession();
            }
        }

        public Repository(ISessionFactory sessionFactory)
        {
            _sessionFactory = sessionFactory;
        }


        /*public IQueryable<T> FindAll(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            return from item in Session.Linq<T>().Where(predicate)
                   select item;
        }

        public IQueryable<T> FindAll(System.Linq.Expressions.Expression<Func<T, bool>> predicate, int pageIndex, int pageSize)
        {
            return (from item in Session.Linq<T>().Where(predicate)
                   select item).Skip<T>((pageIndex-1)*pageSize).Take<T>(pageSize);
        }*/
      

        /// <summary>
        /// Add employee
        /// </summary>
        /// <param name="employee"></param>
        public void Add(T entity)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (session.BeginTransaction())
            {
                session.Save(entity);
                session.Transaction.Commit();
            }
        }

        /// <summary>
        /// Update employee
        /// </summary>
        /// <param name="employee"></param>
        public void Update(T entity)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (session.BeginTransaction())
            {
                session.SaveOrUpdate(entity);
                session.Transaction.Commit();
            }
        }

        public void Remove(T entity)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (session.BeginTransaction())
            {
                session.Delete(entity);
                session.Transaction.Commit();
            }
        }

       /* public void Remove(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            foreach (T entity in Session.Linq<T>().Where(predicate).ToList<T>())
                Remove(entity);
        }*/

        /// <summary>
        /// Deletes every entity that matches the given criteria
        /// </summary>
        /// <param name="criteria"></param>
        public void Remove(DetachedCriteria criteria)
        {
            foreach (T entity in FindAll(criteria))
                Remove(entity);
        }


        public ICollection<T> FindAll(DetachedCriteria criteria)
        {
            return criteria.GetExecutableCriteria(Session).List<T>();
        }

        /// <summary>
        /// Returns each entity that maches the given criteria, and orders the results
        /// according to the given Orders
        /// </summary>
        /// <param name="criteria"></param>
        /// <param name="orders"></param>
        /// <returns></returns>
        public ICollection<T> FindAll(DetachedCriteria criteria, params Order[] orders)
        {
            if (orders != null)
                foreach (Order order in orders)
                    criteria.AddOrder(order);

            return FindAll(criteria);
        }

        /// <summary>
        /// Returns each entity that matches the given criteria, with support for paging,
        /// and orders the results according to the given Orders
        /// </summary>
        /// <param name="criteria"></param>
        /// <param name="firstResult"></param>
        /// <param name="numberOfResults"></param>
        /// <param name="orders"></param>
        /// <returns></returns>
        public ICollection<T> FindAll(DetachedCriteria criteria, int firstResult, int numberOfResults, params Order[] orders)
        {
            criteria.SetFirstResult(firstResult).SetMaxResults(numberOfResults);
            return FindAll(criteria, orders);
        }


        /// <summary>
        /// Returns the one entity that matches the given criteria. Throws an exception if
        /// more than one entity matches the criteria
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public T FindOne(DetachedCriteria criteria)
        {
            return criteria.GetExecutableCriteria(Session).UniqueResult<T>();
        }

        /*public T FindOne(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            return Session.Linq<T>().Where(predicate).FirstOrDefault<T>();//criteria.GetExecutableCriteria(Session).UniqueResult<T>();
        }*/

        /// <summary>
        /// Returns the first entity to match the given criteria
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public T FindFirst(DetachedCriteria criteria)
        {
            IList<T> results = criteria.SetFirstResult(0)
                .SetMaxResults(1)
                .GetExecutableCriteria(Session).List<T>();

            if (results.Count > 0)
                return results[0];

            return default(T);
        }

        /// <summary>
        /// Returns the first entity to match the given criteria, ordered by the given order
        /// </summary>
        /// <param name="criteria"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public T FindFirst(DetachedCriteria criteria, Order order)
        {
            return FindFirst(criteria.AddOrder(order));
        }

        /// <summary>
        /// Returns the total number of entities that match the given criteria
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public long Count(DetachedCriteria criteria)
        {
            return Convert.ToInt64(criteria
                .GetExecutableCriteria(Session)
                .SetProjection(Projections.RowCountInt64()).UniqueResult());
        }

        /// <summary>
        /// Returns the total number of entities that match the given expression
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
       /* public long Count(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            return Convert.ToInt64(Session.Linq<T>().Count(predicate));
        }*/

        /// <summary>
        /// Returns true if at least one entity exists that matches the given criteria
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public bool Exists(DetachedCriteria criteria)
        {
            return Count(criteria) > 0;
        }

        /*public bool Exists(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            return Count(predicate) > 0;
        }*/

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Nokia
Germany Germany
Interested in design/development of framework functionality using the best patterns and practices.

Comments and Discussions