Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 24 Mar 2010 CPOL
This article shows how we can develop the platform-independent software with Mono usage
InfoCenter.zip
InfoCenter
ConfigSamples
InfoCenter.Agent
Agent.pidb
Agent.userprefs
bin
Debug
Agent.exe
Agent.exe.mdb
Agent.pdb
Entities
Helpers
ObjectSerializer.cs~
obj
x86
Debug
TempPE
Service
InfoCenterService.wsdl
InfoCenter.Logic
Agent
Entities
Helpers
bin
Debug
Antlr3.Runtime.dll
Iesi.Collections.dll
InfoCenter.Logic.dll
InfoCenter.Logic.dll.mdb
InfoCenter.Logic.pdb
InfoCenter.Persistence.dll
InfoCenter.Persistence.dll.mdb
InfoCenter.Persistence.pdb
log4net.dll
Release
InfoCenter.Logic.pidb
obj
Debug
InfoCenter.Logic.dll
InfoCenter.Logic.pdb
ResolveAssemblyReference.cache
TempPE
Properties
InfoCenter.Logic.Tests
bin
Debug
Antlr3.Runtime.dll
Castle.Core.dll
Castle.DynamicProxy2.dll
Iesi.Collections.dll
InfoCenter.Logic.dll
InfoCenter.Logic.dll.mdb
InfoCenter.Logic.pdb
InfoCenter.Logic.Tests.dll
InfoCenter.Logic.Tests.dll.mdb
InfoCenter.Logic.Tests.pdb
InfoCenter.Persistence.dll
InfoCenter.Persistence.dll.mdb
InfoCenter.Persistence.pdb
log4net.dll
MySql.Data.dll
NHibernate.ByteCode.Castle.dll
nunit.framework.dll
Release
InfoCenter.Logic.Tests.pidb
obj
Debug
InfoCenter.Logic.Tests.dll
InfoCenter.Logic.Tests.pdb
ResolveAssemblyReference.cache
TempPE
Properties
test-results
InfoCenter.Logic.Tests.csproj.test-cache
InfoCenter.Persistence
bin
Debug
Antlr3.Runtime.dll
Iesi.Collections.dll
InfoCenter.Persistence.dll
InfoCenter.Persistence.dll.mdb
InfoCenter.Persistence.pdb
log4net.dll
Release
Core
Entities
InfoCenter.Persistence.pidb
Mapping
obj
Debug
InfoCenter.Persistence.dll
InfoCenter.Persistence.pdb
ResolveAssemblyReference.cache
TempPE
Properties
InfoCenter.Persistence.Test
bin
Debug
Antlr3.Runtime.dll
Castle.Core.dll
Castle.DynamicProxy2.dll
Iesi.Collections.dll
InfoCenter.Persistence.dll
InfoCenter.Persistence.dll.mdb
InfoCenter.Persistence.pdb
InfoCenter.Persistence.Tests.dll
InfoCenter.Persistence.Tests.dll.mdb
InfoCenter.Persistence.Tests.pdb
log4net.dll
MySql.Data.dll
NHibernate.ByteCode.Castle.dll
NHibernate.dll
nunit.framework.dll
Release
InfoCenter.Persistence.Tests.pidb
obj
Debug
InfoCenter.Persistence.Test.dll
InfoCenter.Persistence.Test.pdb
InfoCenter.Persistence.Tests.dll
InfoCenter.Persistence.Tests.pdb
ResolveAssemblyReference.cache
TempPE
Properties
test-results
InfoCenter.Persistence.Tests.csproj.test-cache
InfoCenter.userprefs
InfoCenter.WebConsole
bin
Antlr3.Runtime.dll
Castle.Core.dll
Castle.DynamicProxy2.dll
Iesi.Collections.dll
InfoCenter.Logic.dll
InfoCenter.Logic.dll.mdb
InfoCenter.Logic.pdb
InfoCenter.Persistence.dll
InfoCenter.Persistence.dll.mdb
InfoCenter.Persistence.pdb
InfoCenter.WebConsole.dll
InfoCenter.WebConsole.dll.mdb
InfoCenter.WebConsole.pdb
log4net.dll
MySql.Data.dll
NHibernate.ByteCode.Castle.dll
Global.asax
InfoCenter.WebConsole.pidb
InfoCenter.WebConsole.userprefs
SharedLibs
Antlr3.Runtime.dll
Castle.Core.dll
Castle.DynamicProxy2.dll
Iesi.Collections.dll
log4net.dll
MySql.Data.dll
NHibernate.ByteCode.Castle.dll
NHibernate.dll
nunit.framework.dll
test-results
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)

Share

About the Author

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 24 Mar 2010
Article Copyright 2010 by Ilya Builuk
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid