Skip to content

4 Queries

Emmanuel D edited this page Nov 9, 2020 · 1 revision

Making queries on repositories is easy as counting up to ten.

Queries can be done by :

  • using the service with method QueryAsync or QueryWithProjectionAsync
  • using a mediatR command : GenericQuery or GenericQueryWithProjectCommand<T, TResult>

Projection methods map entities to a new type. Mapping has to be registered in automapper.

The BaseQuery

Search methods take a BaseQuery as argument.

public class BaseBrandQuery : BaseQuery<T>
{
    public int ID { get; set; }

    public override void BuildQuery()
    {
        if (ID > 0)
        {
            AddFunctionToCriteria(x => x.Id == ID);
        }
    }
}

Query contains properties to search for and a BuildQuery method that will build the query.

The BuildQuery method will process properties and according their values will feed the repository query. AddFunctionToCriteria private method is pushing the function to the query with And (by default) or OR condition.

Include and IncludeStrings properties contain the list of children that the query should embed in the return objects. String or function can be used and full object path is supported. The following sample shows how to get entity with two level of information.

IncludeStrings = new List<string>() { "ChildModel.GrandChildModel" }
Includes = new List<Expression<Func<Model, object>>>() { x => x.ChildModel.GrandChildModel} }

New EF5 include filter is supported.

OrderBy and OrderByStrings properties are list that contain sort information. Full object path is supported.

OrderByStrings = new List<OrderBySpecification<string>>() { new OrderBySpecification<string>("Child.Property", false) }
OrderBy = new List<OrderBySpecification<Expression<Func<Car, object>>>>() { new OrderBySpecification<Expression<Func<T, object>>>(x => x.Child.Property) }

Query support paging through Take and Skip property.

Clone this wiki locally