Create Custom Criterion in NHibernate?

Posted by vbullinger on Stack Overflow See other posts from Stack Overflow or by vbullinger
Published on 2012-06-22T20:50:57Z Indexed on 2012/06/22 21:16 UTC
Read the original article Hit count: 220

Filed under:
|
|

I'm still a bit of a n00b when it comes to NHibernate. Let's say I have the following:

var myCriteria = this.Session.CreateCriteria(typeof(SomeModel)).Add(Restrictions.Eq("SomeProperty", someValue);

Then, let's say I want to add criteria in a way that's reusable. Meaning, I want to make a custom criterion. I'm seeing very, very little information online on this. Specifically, I'd like to turn the following:

var myCriteria = this.Session.CreateCriteria(typeof(SomeModel))
.Add(Restrictions.Eq("SomeProperty", someValue)
.CreateAlias("SomeClass", "alias", JoinType.LeftOuterJoin)
.Add(Restrictions.Eq("alias.SomeOtherProperty", someOtherValue));

Into the following:

var myCriteria = this.Session.CreateCriteria(typeof(SomeModel))
.Add(Restrictions.Eq("SomeProperty", someValue)
.Add(this.GetAliasCriterion());

Thus extracting .CreateAlias("SomeClass", "alias", JoinType.LeftOuterJoin).Add(Restrictions.Eq("alias.SomeOtherProperty", someOtherValue)); into a method.

Is this possible? How does this work?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about nhibernate