NHibernate的:在一个空列一个属性默认值 [英] NHibernate: Default value for a property over a null column

查看:180
本文介绍了NHibernate的:在一个空列一个属性默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和其他应用程序使用的旧数据库工作,所以我不能修改它的结构。
有时我有希望映射为一个非空类型空列。
比如说我有一个数据库,可为空EndValidity列,我想要映射在非可空日期时间,而且,如果为null,它应该是DateTime.MaxValue,或者作为一个更简单的例子,一个可空位列必须如果是假的空

I'm working with an old database used by other applications, so i can't modify its structure. Sometimes i have nullable columns that i want to map as a non nullable type. For instance I have a nullable EndValidity column on database which i want to map on a non nullable DateTime, and, if null, it should be DateTime.MaxValue, or as a more simple example, a nullable bit column that must be false if null.

我正在考虑写一个自定义的用户类型(包括参数如果可能的话),或者是没有包含在NHibernate的为它的东西吗?

I'm thinking about writing a custom user type for it (with parameters if possible), or is it there something included in nHibernate for it?

如果唯一的解决办法是写一个自定义的用户类型,对于布尔的情况下,我可以覆盖fluentnhibernate公约总是适用的用户类型为布尔值?

If the only solution is to write a custom usertype, for the boolean case, can I override fluentnhibernate conventions to always apply that usertype for booleans?

推荐答案

这可能取决于你怎么想你的行为你的POCO类。
首先,如果你加载一个空值你想重新保存为空值,或做你想做的任何会议提交/冲洗的空场新的默认更新的记录。
如果你想从你的POCO的对象只是一个布尔值保持空值在DB你会怎么检测哪个值保存IE布尔值只有true和false选项/值,但是你的位类有选项/值NULL,0和1

It probably depends on how you want your poco class you act. Firstly if you load a null value do you want the null value to be re-saved or do you want any session commit/flush to update the record with the new default for the null field. If you want to keep the null value in the DB how would you detect from your poco object with just a boolean which value to save IE booleans have only true and false options / values but your bit class has the options / values of null, 0 and 1?

如果说:你希望你的可空位作为一个布尔值,而不是一个可空布尔dataype在你的POCO类,并有一流的更新数据库删除所有空再一个解决方案,这是相当快的,而不深入到自定义类型是有两个酒店之一为空的和受保护的或内部,另一个属性与基本逻辑接口(没有任何映射) - 为扶养的原因所有注射POCO类也最好有一个接口或两个:乙EG

If say: you want to have your nullable bits as a boolean and not a nullable boolean dataype in your poco class and have the class update the db to remove all the nulls then one solution which is pretty quick without diving into custom types is to have the two properties one nullable and protected or internal and one property in an interface ( without any mapping ) with basic logic - for reasons of dependancy injects all poco classes would ideally have an interface or two: EG

internal virtual bool? isActive {get;set;}
public bool MyInterface.IsActive 
{
    get{ return isActive ?? false; }
    set{ isActive == value ?? false; };
}



这样,你的逻辑包含在代码,所以如果规则变得越来越复杂/复杂或者一个可空位具有默认设置为FALSE,但另一个是true的默认值(因为从上个世纪一些疯狂的开发者)可以更容易地控制转换。
这将需要您接口而不是真正的POCO类的工作,但同样这不是一件坏事,它只是取决于你的发展模式。

This way your logic is contained in code so if the rules get more complex/complicated or one nullable bit has a default of false but another has a default of true ( because of some insane developer from last century ) you can more easily control the transformations. It will require you working with interfaces instead of the real poco classes, but again this isn't a bad thing, it just depends on your development model.

这篇关于NHibernate的:在一个空列一个属性默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆