Best way to implement Entity with translatable properties in NHibernate -


consider following class (simplified in order focus in core problem):

public class question {     public virtual string questionid { get; set; }      public virtual string text { get; set; }      public virtual string hint { get; set; } } 

and tables:

question - questionid ((primary key, identity column , key) - code  questiontranslation - questiontranslationid (primary key, identity column; not relevant association) - questionid (composite key element 1) - culturename (composite key element 2) (sample value: en-us, en-ca, es-es) - text - hint 

how can map question class text , hint properties populated using current thread's culture. if thread's culture changed text , hint properties automatically return appropriate value without need question entity reloaded.

note i'm outlining relevant class , properties business side. i'm totally open new class or property needed achieve desired functionality.

edited reflect changed answer:

public class question {     public virtual string questionid { get; set; }      public virtual string text     {                 {             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     {                 {             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> 

if have lot of translations change icollection<questiontranslation> translations { get; set; } idictionary<string, questiontranslation> translations { get; set; } , map <map> above should it


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -