开发者

How do I tell NHibernate to load a component as not null even when all its properties are null?

开发者 https://www.devze.com 2022-12-31 07:10 出处:网络
I have a Date class which wraps over the DateTime? class (aids in mocking DateTime.Now, our domain ,etc).

I have a Date class which wraps over the DateTime? class (aids in mocking DateTime.Now, our domain ,etc).

The Date class class only has one protected property : DateTime? date

public class Date
{
    protected DateTime? date;
}

// mapping in hbm
<component name="CompletedOn">
  <property column="StartedOn" name="date" access="field" not-null="false" />
</component>

From the nhibernate docs:

Like all value types, 开发者_StackOverflow中文版components do not support shared references. The null value semantics of a component are ad hoc. When reloading the containing object, NHibernate will assume that if all component columns are null, then the entire component is null. This should be okay for most purposes.

Can I override this behaviour? I want my Date class to be instantiated even if date is null.

Thanks,


I think you could achieve this behavior by using a listener that implements IPostLoadEventListener. After the entity is loaded, you can check if the component is null and new it if it is.


I don't think you can override that behavior.

However, it's usually easier to have NH work as usual (retrieving null) and wrap that in your domain model.

But from what I see, it looks like you are doing this maybe for a presentation (data binding?) concern. Isn't it easier to just expose a DateTime??

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号