使用 NHibernate 将数据库中的 JSON 编码字符串映射到我的对象上的 Dictionary 属性
我有一个对象,它维护各种属性的属性包。它通过我的对象模型作为 Dictionary
公开,并作为 JSON 编码字符串存储在数据库中。
使用 Fluent NHibernate 映射此类的最佳方法是什么?
选项 1:映射到私有字段,在我的对象中进行 JSON 转换?
我是否应该将属性映射到私有 string
字段,然后在 JSON 之间进行序列化我的对象本身?
优点:NH 只需要知道如何将单个字符串映射到文本列,这看起来很简单。
缺点:我的模型必须跟踪字典和字符串,只是为了支持持久性。
选项 2:使用某种拦截器?
我以前没有使用 NH 拦截器做过任何事情,但我知道它们。我可以使用拦截器进行字典/JSON 序列化,以便我的模型只需要了解字典吗?
选项 3:使用不同的编码策略?
除了 JSON 之外,是否还有 NH 本身支持的不同编码策略,并且我可以使用它来序列化我的字典?
I have an object that maintains a property bag of various properties. This is exposed through my object model as a Dictionary<string, string>
and is stored in the database as a JSON-encoded string.
What is the best way to map this class using Fluent NHibernate?
Option 1: Map to a private field, do JSON translation in my object?
Should I map the property to a private string
field, and then do serialization to/from JSON in my object itself?
Pro: NH need only know about mapping a single string to a text column which seems easy enough.
Con: My model has to track the dictionary and a string, simply to support persistence.
Option 2: Use some sort of Interceptor?
I haven't done anything with NH Interceptors before, but I know of them. Could I use an interceptor to do the Dictionary/JSON serialization, so that my model need only know about the dictionary?
Option 3: Use a different encoding strategy?
Is there a different encoding strategy, besides JSON, that NH supports natively and that I could use to serialize my dictionary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了将字典存储为单个编码列,请使用
IUserType
。您可以使用 IUserType 将字典映射为属性。这篇博客文章有一个很好的内容实现 IUserType 的示例。您还可以将其映射为值的集合。这允许从字典中查询各个值。
In order to store your Dictionary as a single encoded column, use a
IUserType
. You would map the dictionary as a property using your IUserType. This blog post has a good example of implementing an IUserType.You can also map this as a collection of values. This allows querying of individual values from the dictionary.