Skip to content
geoffreysmith edited this page Jun 1, 2011 · 2 revisions

Recipe #002 - Using IQuery<T>

Overview

Inspiration: Udi Dahan IQuery vs Methods on the Repository

IQuery<T> is meant to replace methods on the repository, and lives in the Tasks Layer. Queries can then be passed down to lower layers to help populate viewModels, reporting or for usage in other applications. The contracts live in the Domain Layer and the implementation lives in the Infrastructure Layer. The convention is one file per query.

Example Implementation

IQuery

// One file per Query
public class ProductsForSaleQuery : NHibernateQuery<Product>, IProductsForSaleQuery
{
    public override IList<Product> ExecuteQuery()
    {
        return (from product in Session.Query<Product>()
                where product.SellEndDate.Date > DateTime.Parse("5/30/2003")
                select product).ToList();
    }
}

Contract

public interface IProductsForSaleQuery : IQuery<Product>
{
}

Tasks

    public IList<Product> GetProductsForSale()
    {
        return this.productRepository.PerformQuery(this.productsForSaleQuery);
    }