I was able to achieve that using NHibernate.Cfg.Configuration:
var cfg = new Configuration();
var filterDef = new FilterDefinition(
"filterName",
null, // or your default condition
new Dictionary<string, IType> { { "filterParamName", NHibernateUtil.Int32 } },
false);
cfg.AddFilterDefinition(filterDef);
// cfg.AddMapping(...)
// cfg.DataBaseIntegration(...)
var sessionFactory = cfg.BuildSessionFactory();
then define the filter in entity mapping:
public class EntityMap : ClassMapping<Entity>
{
public EntityMap()
{
Table("Entity");
Filter("filterName", m => m.Condition("FilteredField = :filterParamName"));
// remaining mapping
}
}
and then use it as follows:
using(var session = sessionFactory.OpenSession())
{
var filterValue = 123;
session
.EnableFilter("filterName")
.SetParameter("filterParamName", filterValue);
}
I hope you;ll find this useful.