gpt4 book ai didi

nhibernate - 在 NHibernate 中实现具有可翻译属性的实体的最佳方法

转载 作者:行者123 更新时间:2023-12-04 06:19:25 24 4
gpt4 key购买 nike

考虑以下类(为了关注核心问题而进行了简化):

public class Question
{
public virtual string QuestionId { get; set; }

public virtual string Text { get; set; }

public virtual string Hint { get; set; }
}

和表:
Question
- QuestionId ((primary key, identity column and key)
- Code

QuestionTranslation
- QuestionTranslationId (primary key, identity column; not really relevant to the association)
- QuestionId (composite key element 1)
- CultureName (composite key element 2) (sample value: en-US, en-CA, es-ES)
- Text
- Hint

如何映射 Question 类,以便使用当前线程的区域性填充 Text 和 Hint 属性。如果线程的文化发生变化,我希望 Text 和 Hint 属性自动返回适当的值,而无需重新加载 Question 实体。

请注意,我只是从业务方面概述了相关的类和属性。我对实现所需功能所需的任何新类或属性完全开放。

最佳答案

编辑以反射(reflect)更改后的答案:

public class Question
{
public virtual string QuestionId { get; set; }

public virtual string Text
{
get
{
var currentculture = CultureInfo.CurrentCulture.Name;
return Translations
.Where(trans => trans.CultureName == currentculture)
.Select(trans => trans.Text)
.FirstOrDefault();
}
set
{
var currentculture = CultureInfo.CurrentCulture.Name;
var translation = Translations
.Where(trans => trans.CultureName == currentculture)
.FirstOrDefault();
if (translation == null)
{
translation = new QuestionTranslation();
Translations.Add(translation);
}
translation.Text = value;
}
}
public virtual string Hint
{
get
{
var currentculture = CultureInfo.CurrentCulture.Name;
return Translations
.Where(trans => trans.CultureName == currentculture)
.Select(trans => trans.Hint)
.FirstOrDefault();
}
set
{
var currentculture = CultureInfo.CurrentCulture.Name;
var translation = Translations
.Where(trans => trans.CultureName == currentculture)
.FirstOrDefault();
if (translation == null)
{
translation = new QuestionTranslation();
Translations.Add(translation);
}
translation.Hint = value;
}
}

protected virtual ICollection<QuestionTranslation> Translations { get; set; }
}

class QuestionTranslation
{
public virtual int Id { get; protected set; }
public virtual string CultureName { get; set; }
public virtual string Text { get; set; }
public virtual string Hint { get; set; }
}

<class name="Question" xmlns="urn:nhibernate-mapping-2.2">
<id name="QuestionId" column="QuestionId"/>

<bag name="Translations" table="QuestionTranslation" lazy="true">
<key>
<column name="QuestionId"/>
</key>
<one-to-many class="QuestionTranslation"/>
</bag>
</class>

<class name="QuestionTranslation" table="QuestionTranslation" xmlns="urn:nhibernate-mapping-2.2">
<id name="QuestionTranslationId"/>
<many-to-one name="ParentQuestion" column="QuestionId"/>
</class>

如果你有很多翻译,那就换 ICollection<QuestionTranslation> Translations { get; set; }IDictionary<string, QuestionTranslation> Translations { get; set; }并映射为 <map>但通常上面应该这样做

关于nhibernate - 在 NHibernate 中实现具有可翻译属性的实体的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6782823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com