How to add filter definitions with display of Nhibernate 3.2 code?

ModelInspector does not seem to provide a means to define filter definitions. Any ideas / workarounds?

I need to generate the following with code display:

<filter-def name="filterName" use-many-to-one="false">
  <filter-param name="filterParamName" type="Int32"/>
</filter-def>
+5
source share
2 answers

I was able to achieve this with 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 the entity mapping:

public class EntityMap : ClassMapping<Entity>
{
    public EntityMap()
    {
        Table("Entity");
        Filter("filterName", m => m.Condition("FilteredField = :filterParamName"));
        // remaining mapping
    }
}

and then use it like this:

using(var session = sessionFactory.OpenSession())
{
    var filterValue = 123;
    session
        .EnableFilter("filterName")
        .SetParameter("filterParamName", filterValue);
}

Hope you find this helpful.

+8
source

FYI,

It is important to note that the call is AddFilterDefinitionup AddMapping, otherwise you will get ArgumentException("An item with the same key has already been added")!

+6
source

All Articles