我们正在尝试设计一个支持多语言数据的站点,我们使用 asp.net 和 nhibernate 进行 O/R 映射。经过一番谷歌搜索后,我们决定最好为我们创建包含支持默认(英语)语言所需的所有字段的实体类,并且对于每个多语言类,我们将创建一个仅包含多语言字段和主字段 id 的新类(英语)类和语言 ID。例如一个简单的“产品”类,我们可以有以下字段:
产品
int ID
字符串 DescriptionInEnglish
字符串 FullDescriptionInEnglish
十进制价格
第二类“product_Lang”包含
产品_Lang
int Product_ID
int Language_ID
string Description
string FullDescription
然后,为了能够以任何语言加载产品,我们可以在 产品上添加一个名为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 属性。这可能很容易,但我无法弄清楚。它不是一对一的,因为英语语言是可选的。我考虑过一对多,所以我加载了所有语言的列表,但我认为加载所有语言是不公平的,因为我只需要一种。
有什么帮助吗?或者任何其他建议仍然会给出公平的表现?