Skip to content

Instantly share code, notes, and snippets.

@agross
Created July 24, 2009 09:17
Show Gist options
  • Save agross/153931 to your computer and use it in GitHub Desktop.
Save agross/153931 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using Indigo.Domain.Model.Employees;
using Indigo.Tools;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.LambdaExtensions;
using Rhino.Commons;
namespace Indigo.Domain.Queries.Employees
{
public class AllEmployees : Query<Employee, IEnumerable<Employee>>
{
public override DetachedCriteria Criteria
{
get { return DetachedCriteria.For<Employee>(); }
}
public override IEnumerable<Employee> Execute(IRepository<Employee> repository)
{
return repository.FindAll(Criteria);
}
}
public struct EmployeeSearchCriteria
{
public int? CostCenter
{
get;
set;
}
public int? EmployeeNumber
{
get;
set;
}
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
}
public class EmployeeSearch : AllEmployees
{
readonly EmployeeSearchCriteria _criteria;
public EmployeeSearch(EmployeeSearchCriteria criteria)
{
_criteria = criteria;
}
public override DetachedCriteria Criteria
{
get
{
var criteria = DetachedCriteria.For<Employee>();
if (_criteria.FirstName.HasValue())
{
criteria.Add(SqlExpression.InsensitiveLike<Employee>(x => x.Name.FirstName, _criteria.FirstName, MatchMode.Anywhere));
}
if (_criteria.LastName.HasValue())
{
criteria.Add(SqlExpression.InsensitiveLike<Employee>(x => x.Name.LastName, _criteria.LastName, MatchMode.Anywhere));
}
if (_criteria.CostCenter.HasValue)
{
criteria.Add<Employee>(x => x.CostCenter.Value == _criteria.CostCenter);
}
if (_criteria.EmployeeNumber.HasValue)
{
criteria.Add<Employee>(x => x.EmployeeNumber.Value == _criteria.EmployeeNumber);
}
return criteria;
}
}
public override IEnumerable<Employee> Execute(IRepository<Employee> repository)
{
return repository.FindAll(Criteria);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment