如何分配数据层级过滤器 [英] how to assign data layer-level filters

查看:126
本文介绍了如何分配数据层级过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,问题是一些如何怪异。

first, the question is some how weird.

我设计使用ASP.NET MVC,SQL-Server和NHibernate的ORM作为一个Web应用程序,在设计应用程序,我发现应用程序数据分为两部分(根据用户是男性或女性)所以我有两个选择,当你选择其中一个所有的应用数据将被分为两部分(基于性别的过滤器)

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)

那么,有没有把一些网来过滤基于这样的过滤器从数据库中的数据的方法吗?像性别,位置,还是分公司?如何使用这些数据库级过滤器?并且无需使用... WHERE SEX = 1到处都在我的应用程序,数据库级只有一个过滤器?

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?

推荐答案

NHibernate的有作为desrcribed的 18.1。 NHibernate的过滤器

NHibernate has a feature desrcribed as 18.1. NHibernate filters.

这是它可以在的级别应用设置。从documenation例子:

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>

,或到集合:

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

有了这个映射我们的实体/ collecitons设置,我们可以应用过滤器上的ISession平:

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

下面是一些例子:

  • NHibernate QueryOver - collection with too many results (xml mapping)
  • FluentNHibernate - Mapping a class to multiple tables (fluent nhibernate)

我发现用于收藏作为一种非常powrful功能这些过滤器。集合/词典可以包含一些用户的相关数据(例如,按地理位置,语言,角色等过滤器)

另一种方法可能只是添加过滤器ALA查询:。新增(ICriterion)(标准查询)。凡(例如pression)(QueryOver)的。这可能会在AOP过滤器包裹你的DAL完成。

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.

这篇关于如何分配数据层级过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆