1

first, the question is some how weird.

I'm designing a web application using ASP.NET MVC, SQL-Server and NHibernate as ORM, while designing the application I discovered that the application data is divided into two parts (based on whether the user is Male or Female) so I have two options which when you choose one of them ALL application data will be divided into two sections (based on Sex filter)

So, is there any way of putting some "net" to filter the data from DB based on such filters? like Sex, Location, or Branch? how to use such database level filters? and no need to use "... WHERE SEX=1" everywhere in my application, only one filter on the database level?

magdy.roshdy
  • 69
  • 1
  • 7

2 Answers2

1

Class mapping has a where property. If you use it all queries targeting that class will include your where condition.

emperon
  • 369
  • 3
  • 11
1

NHibernate has a feature desrcribed as 18.1. NHibernate filters.

This is a setting which could be applied on the class or the collection level. Examples from the documenation:

<class name="MyClass" ...>
    ...
    // this way we are saying, retrieve instances which meet the criteria
    <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/>
</class>

or, to a collection:

<set ...>
    <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/>
</set>

Having this mapping set on our entities/collecitons, we can apply the filter on the ISession level:

session.EnableFilter("myFilter").SetParameter("myFilterParam", "some-value");

And any mapped object is now filtered with this setting

Here are some examples:

I found these filters used for collections as a very powrful feature. Collections/dictionaries can contain some "user" dependent data (e.g. the filter by location, language, role etc)

Another approach could be just adding the filter to queries ala: .Add(ICriterion) (Criteria query) or .Where(expression) (QueryOver). This could be done in the AOP filter wrapping your DAL.

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335