How to implement correctly IUserType?

一世执手 提交于 2019-12-17 11:27:30

问题


I need to create a custom type for NHibernate by writing a new mapper class that implements IUserType. While it is relatively straightforward to override most of the methods and properties, I get some difficulties to understand how to deal correctly with the following members:

  • object Assemble(object cached, object owner);
  • object DeepCopy(object value);
  • object Disassemble(object value);
  • object Replace(object original, object target, object owner);

I do not understand what is exactly their purpose; and more important, how to properly implement them. Most of the examples I have seen just return the raw input parameter.

public object DeepCopy(object value)
{
    return value;
}

public object Replace(object original, object target, object owner)
{
    return original;
}

public object Assemble(object cached, object owner)
{
    return cached;
}

public object Disassemble(object value)
{
    return value;
}

How to implement those methods correctly in a real case or more complex scenario?


回答1:


Have a look at how Ritesh Rao has done this in his NCommon framework:

The MoneyUserType implements a base class called CompositeUserTypeBase

There's more detail in the comments but to summarize:

  • DeepCopy - should return a deep copy of the persistent state, stopping at entities and at collections
  • Disassemble - transforms the object into its 'cacheable' representation (i.e. associations must be cached as identifier values)
  • Assemble - reconstructs an object from the cacheable representation

It's explained in a lot more detail here




回答2:


You may want to check out this article. It is a good example of how to implement the IUserType interface.



来源:https://stackoverflow.com/questions/1565056/how-to-implement-correctly-iusertype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!