Lite Query is an implementation of the Query Object Pattern that will help you to build the queries in the Frontend layer in a way that will be independent from the ORM used in Data Access. The object is translated in the language of the ORM framework using Query Translators. This version comes with translators for Entity Framework and NHibernate.

Quick Start

Create a query
using Lib.Query;

LiteQuery<Policy> query = new LiteQuery<Policy>();
query.Add(x => x.Number, CriteriaOperator.Equal, 1);
IList<Policy> list = repository.FindBy<Policy>(query);

Translate the query for Entity Framework
using Lib.Query.EntityFramework;

public IList<T> FindBy<T>(IReadLiteQuery query) where T:Policy
{
    InsuranceSampleEntities context = new InsuranceSampleEntities();
    return query.Translate<T>(context.Policies.OfType<T>()).ToList<T>();
}  

Translate the query for NHibernate
using Lib.Query.NHibernate;

public IList<T> FindBy<T>(IReadLiteQuery query) where T:Policy
{
     ICriteria criteriaQuery = NHSessionFactory.Context.CreateCriteria(typeof(T));
     criteriaQuery = query.Translate<T>(criteriaQuery);
     return criteriaQuery.List<T>();
}  


Another way to make a search is by using predefined queries.
Create a predefined query
[Serializable()]
public class PolicyQuery : LiteQuery<Policy>
{
     public PolicyQuery()
     {
         this.OrderBy(x => x.Id, false);
      }

     private int _id;        
     public int Id 
     {
         get { return _id; }
         set 
         {
             _id = value;
              this.Add<Policy>(x => x.Id, CriteriaOperator.Equal, value); 
          }
      }

Use a predefined query
PolicyQuery query = new PolicyQuery();
query.Number = 2;

IList<Policy> list = repository.FindBy<Policy>(query);

Last edited Jun 21, 2011 at 6:49 PM by cristi_salcescu, version 18