Skip to header Skip to main navigation Skip to main content Skip to footer

User account menu

  • Log in
Amaël BERTEAU
Software Craftsman & Maker

Main navigation

  • Home
  • News
  • Software Engineering
  • Maker
  • Entrepreneuriat
  • Misc
  • Personal Projects
    • Bio
    • Curriculum Vitae
    • Skills
    • Likes
    • My Values
    • Quotes
    • Favorite Books
    • Favorite Tools

Improve NHibernate QueryOver with Restrictions and ICriterion

Breadcrumb

  • Home
  • Software Engineering
  • Improve NHibernate QueryOver with Restrictions and ICriterion

Software Engineering

  • Agile
  • Concepts / Practices
  • Architecture / Design
  • Implementation
  • Tools
    • FR
    • EN
By aberteau | 10:39 PM CEST, Fri July 21, 2017

Goals

Produce compact and comprehensive code using NHibernate QueryOver, Restrictions and ICriterion.

Context

We will take an example with an entity Period, defined by a name, a start date and a end date.

Period

Needs

We want to request database to get periods corresponding some criteria :

  • all the periods that contains a date
  • all the periods that overlap another period

Implementation

We will use PeriodCriteriaBuilder to build the ICriterion blocks :

Some methods build small criteria :

  • BuildBeginBeforeOrEqualCriterion(DateTime date)
  • BuildEndAfterOrEqualCriterion(DateTime date)
  • BuildBeginBetweenCriterion(DateTime start, DateTime end)
  • BuildEndBetweenCriterion(DateTime start, DateTime end)

Then we can combine them with logical Restrictions expression to provide more advanced predicates :

  • BuildBeginOrEndBetweenCriterion(DateTime start, DateTime end)
  • BuildContainsDateCriterion(DateTime date)
  • BuildContainsStartOrEndDateCriterion(DateTime start, DateTime end)
  • BuildOverlapsPeriodCriterion(DateTime start, DateTime end)

PeriodCriteriaBuilder

QueryOverPeriod

Usage

NHibernate
QueryOver
.Net

Footer menu

  • Contact

Copyright © 2026 Amael BERTEAU - All rights reserved