使用 NHibernate 转变传统价值
我们有一些带有遗留模式的旧表,我们发现它们很难使用。
是否可以使用 NHibernate 将其中一些数据转换为更好的模型?
例如,我们可能有一个整数状态列,我们希望将其分解为多个属性。
Legacy status: 0 - Active, 1 - Inactive, 2 - TemporarilyInactive, 3 - etc
我们想要这样的东西:(
bool IsActive { get; set; }
Status Status { get; set; }
其中 Status
是一个枚举)
我知道我们可以使用一个 protected
字段来获取状态,然后定义 getter额外的属性根据支持字段返回适当的值,但我很确定这将禁用基于这些属性进行查询的能力。
然而,通过这种方式,我们将无法执行诸如 .Query(p => p.IsActive)
之类的查询并将其转换为 SQL,例如 where status = 0
代码>,对吗?
是通过自定义 IUserTypes
的唯一方法吗?如果是这样,是否有任何帮助程序框架可以使使用 IUserType
更轻松地实现这一目标?
其他人如何处理这个问题?
We have some old tables with legacy schemas that we find it hard to work with.
Is it possible to use NHibernate to transform some of this data into a nicer model?
For example we might have an integer status column that we want to break down into several properties.
Legacy status: 0 - Active, 1 - Inactive, 2 - TemporarilyInactive, 3 - etc
We'd want to have something like:
bool IsActive { get; set; }
Status Status { get; set; }
(where Status
is an enum)
I know that we can use a protected
field that can take the status and then define the getters for the extra properties to return the appropriate value based on the backing field, but I'm pretty sure that this will disable the ability to query based on these properties.
Through this however, we wouldn't be able to do queries such as .Query(p => p.IsActive)
and get that translated to SQL such as where status = 0
, right?
Is the only way through custom IUserTypes
? If so, is there any helper framework that makes working with IUserType
easier in order to achieve this?
How do other people handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建自己的“枚举”类。
然后,您可以让 NHibernate 使用旧值设置一个私有变量,并使用一个 getter/setter 在枚举和旧值之间进行转换。
然后,您可以在 NHibernate 查询中使用此枚举:
.Query(p => p.Status.Code)
You can create your own "enumeration" class.
Then you can have NHibernate set a private variable with the legacy value and have a getter/setter that converts between the enumeration and the legacy value.
You can then use this enumeration in NHibernate queries:
.Query(p => p.Status.Code)