Syntax to define a NHibernate Filter with Fluent Nhibernate?

后端 未结 3 1854
Happy的楠姐
Happy的楠姐 2020-11-27 05:52

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

相关标签:
3条回答
  • 2020-11-27 06:27

    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.

    0 讨论(0)
  • 2020-11-27 06:32

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

    0 讨论(0)
  • 2020-11-27 06:34

    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");
    
    0 讨论(0)
提交回复
热议问题