使用ASP.NET,NHibernate的/功能NHibernate最无痛的多租户实现 [英] Most painless multi-tenancy implementation using ASP.NET, NHibernate / Fluent NHibernate

查看:226
本文介绍了使用ASP.NET,NHibernate的/功能NHibernate最无痛的多租户实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个ASP.NET MVC应用程序,我有,使用NHibernate的多租户。虽然我有过数据库多租户的控制。我试图找出使用NHibernate来过滤数据库查询的最佳方式。

I'm trying to implement multi-tenancy in an ASP.NET MVC application I have, that uses NHibernate. Though I have control over the database for multi-tenancy. I'm trying to figure out the best way to filter our database queries using NHibernate.

我想知道是否有一个无痛的方式,我可以追加条件(类似 WHERE实例id = 1 )使用NHibernate的每一个CRUD查询到DB

I would like to know if there is a painless way where I can append a condition (something like WHERE InstanceID = 1) to every CRUD query to the DB using NHibernate.

我看着全局过滤器。但我不知道,如果我用正确的方式。我想这样的事情。

I looked at global filters. But I wasn't sure if I'm using it the right way. I tried something like this.

NHibernateSession.GetDefaultSessionFactory().GetCurrentSession()
.EnableFilter("instance-filter").SetParameter("InstanceId", "2");

但它似乎没有工作。 NHibernate的的全局筛选任何好的例子/使用条件过滤所有的数据库查询什么好方法是非常AP preciated。

But it didn't seem to work. Any good example of NHibernate global filters / any good approach of filtering all DB queries with a condition would be highly appreciated.

推荐答案

我一直在找我的一个小项目,该项目仍处于规划阶段同样的事情。最完整的实现使用我来到由迈克尔Valenty 在他的博客中写了一个单一的数据库:<一个href=\"http://www.agileatwork.com/bolt-on-multi-tenancy-in-asp-net-mvc-with-unity-and-nhibernate-part-ii-commingled-data/\"相对=nofollow>螺栓固定的多租户在ASP.NET MVC使用Unity和NHibernate:第二部分 - 混合数据。他还使用全局过滤器。

I've been looking for the same thing for a small project of mine that's still in planning phase. The most complete implementation of using a single database that I came upon is written by Michael Valenty in his blog post: Bolt-on Multi-Tenancy in ASP.NET MVC with Unity and NHibernate: Part II – Commingled Data. He's also using global filters.

只是为了完整起见,这里是他使用的映射:

Just for the sake of completeness, here are the mappings he used:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <filter-def name="tenant">
        <filter-param name="id" type="System.Int32" />
    </filter-def>
</hibernate-mapping>

和为每个实体:

<class name="User" table="[user]">
    <id name="Id" column="user_id">
        <generator class="identity" />
    </id>

    <property name="Username" />
    <property name="Email" />

    <filter name="tenant" condition="tenant_id = :id" />
</class>

在那之后,他用他选择的IoC容器的参数值注入到的ISession实例。

After that, he uses his IoC container of choice to inject the parameter value to instance of ISession.

session.EnableFilter("tenant").SetParameter("id", c.Resolve<Tenant>().Id);

还有实现一个拦截器 - 保存实体(的OnSave 法)时写电流租客id的值,并且也检查指定的实体是否属于目前的租户通过加载ID的实体时(的OnLoad 方法)。 的OnLoad 覆盖是必要的,因为通过ID载入实体时,承租人过滤器将不会被应用。

There's also an interceptor to implement - to write the value of current tenant id when saving the entity (OnSave method), and also to check whether the given entity belongs to current tenant when loading the entity by id (OnLoad method). OnLoad override is necessary because tenant filter won't be applied when loading entity by id.

这篇关于使用ASP.NET,NHibernate的/功能NHibernate最无痛的多租户实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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