Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax to define a NHibernate Filter with Fluent Nhibernate?

Tags:

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>  
like image 337
Drevak Avatar asked Jun 06 '09 21:06

Drevak


People also ask

What is the difference between NHibernate and fluent NHibernate?

Fluent NHibernate offers an alternative to NHibernate's standard XML mapping files. Rather than writing XML documents, you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code.


Video Answer


2 Answers

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"); 
like image 100
snicker Avatar answered Sep 20 '22 20:09

snicker


In case anyone's still watching this, I've just submitted a patch on Google code for Fluent NHibernate to support filters. You can see it in use in snicker's answer above.

like image 44
David M Avatar answered Sep 21 '22 20:09

David M