Filtering queries with AOP?

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());
 }
Advertisements
Explore posts in the same categories: nhibernate

Tags: , ,

You can comment below, or link to this permanent URL from your own site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: