问题描述
我需要创建一个 自定义类型用于NHibernate,通过编写一个实现了IUserType
的新映射器类.虽然覆盖大多数方法和属性相对简单,但我在理解如何正确处理以下成员时遇到了一些困难:
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;
}
如何在真实案例或更复杂的场景中正确实现这些方法?
推荐答案
看看 Ritesh Rao 如何在他的 NCommon 框架:
Have a look at how Ritesh Rao has done this in his NCommon framework:
MoneyUserType 实现了一个名为 CompositeUserTypeBase
评论中有更多细节,但要总结一下:
There's more detail in the comments but to summarize:
- DeepCopy - 应该返回持久状态的深层副本,在实体和集合处停止
- 反汇编 - 将对象转换为其可缓存"表示(即关联必须作为标识符值缓存)
- 组装 - 从可缓存的表示中重建一个对象
这篇关于如何正确实现 IUserType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!