【发布时间】:2009-07-21 19:10:31
【问题描述】:
我们正在尝试设计一个支持多语言数据的网站,我们使用 asp.net 和 nhibernate 进行 O/R 映射。经过一番谷歌搜索后,我们决定最好为我们创建包含支持默认(英语)语言所需的所有字段的实体类,并且对于每个多语言类,我们将创建一个仅包含多语言字段和主字段 id 的新类(英语)类和语言 ID。例如一个简单的“产品”类,我们可以有以下字段:
产品
int ID
字符串 DescriptionInEnglish
字符串 FullDescriptionInEnglish
十进制价格
以及包含
的第二类“product_Lang”product_Lang
int Product_ID
int Language_ID
字符串说明
字符串完整描述
然后,为了能够加载任何语言的产品,我们可以在 products 上添加一个名为 lang 的 product_Lang 属性,以便于绑定我们可以有两个这样的只读属性:
string DescriptionToBind
{
get
{
if (lang != null)
return this.lang.Description;
else
return this.DescriptionInEnglish;
}
}
string FullDescriptionToBind
{
get
{
if (lang != null)
return this.lang.FullDescription;
else
return this.FullDescriptionInEnglish;
}
}
为了加载一些产品,我们可以像这样在 ProductRpository 上使用一些方法:
Product GetProductByID(int ID);
Product GetProductbyID_ML(int ID, int Language_ID);
以及用于加载一些集合
IList<Product> GetAllProducts();
IList<Product> GetAllProducts_ML(Language_ID);
问题是如何在 nhibernate 中映射产品类中的 Lang 属性。 这可能很容易,但我无法弄清楚。它不是一对一的,因为英语语言是可选的。我考虑过一对多,所以我加载了所有语言的列表,但我认为加载所有语言是不公平的,因为我只需要一种。
有什么帮助吗?或任何其他建议仍能提供公平的表现?
【问题讨论】:
标签: asp.net nhibernate nhibernate-mapping domain-driven-design multilingual