如何映射 IDictionary在流利的 NHibernate 中?
我希望将用户首选项保留到名称值对的集合中,其中值可以是 int、bool 或 string。
有几种方法可以给这只猫剥皮,但我能想到的最方便的方法是这样的:
public class User
{
public virtual IDictionary<string, object> Preferences { get; set; }
}
其用法如下:
user.Preferences["preference1"] = "some value";
user.Preferences["preference2"] = 10;
user.Preferences["preference3"] = true;
var pref = (int)user.Preferences["preference2"];
我不确定如何在 Fluent NHibernate 中映射它,尽管我确实认为这是可能的。
一般来说,您会映射一个更简单的 Dictionary
HasMany(x => x.Preferences)
.Table("Preferences")
.AsMap("preferenceName")
.Element("preferenceValue");
但是对于“对象”类型,NHibernate 不知道如何处理它。我想象可以创建一个自定义 UserType,将“对象”分解为表示其类型的字符串和表示值的字符串。我们会有一个看起来像这样的表:
Table Preferences
userId (int)
preferenceName (varchar)
preferenceValue (varchar)
preferenceValueType (varchar)
hibernate 映射会像这样:
<map name="Preferences" table="Preferences">
<key column="userId"></key>
<index column="preferenceName" type="String" />
<element type="ObjectAsStringUserType, Assembly">
<column name="preferenceValue" />
<column name="preferenceValueType"/>
</element>
</map>
我不确定如何在 Fluent NHibernate 中映射它。
也许有更好的方法来做到这一点,或者也许我应该忍住并使用 IDictionary
I am looking to persist user preferences into a collection of name value pairs, where the value may be an int, bool, or string.
There are a few ways to skin this cat, but the most convenient method I can think of is something like this:
public class User
{
public virtual IDictionary<string, object> Preferences { get; set; }
}
with its usage as:
user.Preferences["preference1"] = "some value";
user.Preferences["preference2"] = 10;
user.Preferences["preference3"] = true;
var pref = (int)user.Preferences["preference2"];
I'm not sure how to map this in Fluent NHibernate, though I do think it is possible.
Generally, you would map a simpler Dictionary<string, string> as:
HasMany(x => x.Preferences)
.Table("Preferences")
.AsMap("preferenceName")
.Element("preferenceValue");
But with a type of 'object', NHibernate doesn't know how to deal with it. I imagine a custom UserType could be created that breaks an 'object' down to a string representing its Type and a string representing the value. We would have a table that looks kind of like this:
Table Preferences
userId (int)
preferenceName (varchar)
preferenceValue (varchar)
preferenceValueType (varchar)
and the hibernate mapping would like this:
<map name="Preferences" table="Preferences">
<key column="userId"></key>
<index column="preferenceName" type="String" />
<element type="ObjectAsStringUserType, Assembly">
<column name="preferenceValue" />
<column name="preferenceValueType"/>
</element>
</map>
I'm not sure how you would map this in Fluent NHibernate.
Maybe there's a better way to do this, or maybe I should just suck it up and use IDictionary<string, string>. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说
IDictionary
会容易得多。然而这是代码i would say
IDictionary<string,string>
would be a lot easier. However here's the code