Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling Nhibernate filters by default

Tags:

c#

nhibernate

Is there a way to make sure a filter (<filter-def>) in enabled by default as opposed to having to call session.EnableFilter("filter_name") everytime?

like image 508
Khash Avatar asked Feb 01 '26 00:02

Khash


1 Answers

I understand this may not exactly solve your issue, but this may be done if you wire your objects via an IOC container or if you have a single point where you create the session.

How I've handled it is on activation of ISession I've toggled the filter by default (using Autofac):

        builder.RegisterAdapter<ISessionFactory, ISession>(factory => factory.OpenSession())
            .InstancePerHttpRequest()
            .OnActivated(activatedArgs =>
                         {
                             var session = activatedArgs.Instance;
                             session.EnableFilter(MyCustomFilter.Name);
                             session.BeginTransaction();
                         });
like image 100
Bruno Lopes Avatar answered Feb 03 '26 12:02

Bruno Lopes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!