Khash
Khash

Reputation: 2570

Enabling Nhibernate filters by default

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?

Upvotes: 3

Views: 1705

Answers (1)

Bruno Lopes
Bruno Lopes

Reputation: 3056

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();
                         });

Upvotes: 4

Related Questions