Snowing Code

Personal notes on software development

Filtering queries with AOP?

(Publish date: 25/12/2009)

Our client asked us to enable the filtering of certain queries by the profile of the current logged user. For example, a user from a certain region could see information concerning his region only. The first thing that came into my mind was setting the context with AOP; however, using NHibernate, I was kindly directed by Tuna Toksöz (thanks again!) to this truly elegant solution. However, this work only when the context is “prefixed” and known in advanced. When in need to enable a more dynamic approach, where the (admin) users could configure themselves what filters should be enabled on which users, have a look at Ayende’s way of doing this.

One thing you should bear in mind- if your context is an enum, they are mapped by default to a string/nvarchar column by NH today, so that adding the filter parameter type would be:

filterParametersType.Add("current", NHibernateUtil.String);

And the filter enabling in your Interceptor would be

public override void SetSession(ISession session)
           session.EnableFilter("contextFilter").SetParameter("current", Context.Current.ToString());
blog comments powered by Disqus