views:

1368

answers:

4

It seems I can't find the correct syntax to define a nhibernate filter using fluent Nhibernate.

I'm trying to follow this ayende's blogpost:

http://ayende.com/Blog/archive/2006/12/26/LocalizingNHibernateContextualParameters.aspx

I defined the formula on my property with .FormulaIs() method but can't find on google how to translate this definition to fluent nhibernate:

 < filter-def name='CultureFilter'>
   < filter-param name='CultureId' type='System.Int32'/>
 < /filter-def>
+1  A: 

This recent post in the Fluent NHibernate discussion leads me to believe that filters are not yet supported by the Fluent API.

Jamie Ide
+2  A: 

In case anyone's still watching this, I've just submitted a patch on Google code for Fluent NHibernate to support filters.

David M
+6  A: 

If you build Fluent from source, there is now support for filters. You use them like this:

First create a class inheriting from FluentNHibernate.Mapping.FilterDefinition:

using FluentNHibernate.Mapping;

namespace PonyApp.FluentFilters
{
    public class PonyConditionFilter : FilterDefinition
    {
        public PonyConditionFilter()
        {
            WithName("PonyConditionFilter")
                .AddParameter("condition",NHibernate.NHibernateUtil.String);
        }
    }
}

In your ClassMap for your class, use the ApplyFilter method:

namespace PonyApp.Entities.Mappings
{
    public class PonyMap : ClassMap<Pony>
    {
        public PonyMap()
        {
            Id(x => x.Id);
            Map(x => x.PonyName);
            Map(x => x.PonyColor);
            Map(x => x.PonyCondition);
            ApplyFilter<PonyConditionFilter>("PonyCondition = :condition");
        }
    }
}

Then add the filter to your fluent config:

Fluently.Configure()
    .Mappings(m => m.FluentMappings.Add(typeof(PonyConditionFilter)))
    //blah blah bunches of other important stuff left out
    .BuildSessionFactory();

Then you can turn it on and off just as you would with vanilla NHibernate:

session.EnableFilter("PonyConditionFilter").SetParameter("condition","Wonderful");
snicker
A: 

public FluentSpecificRevisionFilter()

is this supposed to be a constructor? or what is the return value for the method?

ok....it is the constructor...should be public PonyConditionFilter()

also...when you apply the filter ApplyFilter("PonyCondition = :condition");

the left side of the assignment needs to be the actual column name in your table...not the NHibernate name....
so... Map(x => x.MyColumn).Column("xyz_Column); would be... ApplyFilter("xyz_Column = :condition");

very nice post....works like a charm!

Kenny
Yes.. You might want to delete this answer and add it as a comment to mine to prevent confusion. I also edited mine to fix the constructor name, that was an accident from sanitizing some of my source :P
snicker