如何正确地贯彻执行IUserType? [英] How to implement correctly IUserType?
问题描述
我需要创建一个<一个href=\"https://www.hibernate.org/hib%5Fdocs/nhibernate/1.2/reference/en/html/mapping.html#mapping-types-custom\">custom通过编写一个新的映射器类实现 IUserType
键入了解 NHibernate的。虽然它是相对简单的覆盖大部分的方法和属性,我得到了一些困难,了解如何与以下成员正确处理:
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:
-
对象装配(对象缓存,对象所有者);
-
对象正是deepcopy(对象的值);
-
对象拆解(对象的值);
-
替换对象(物体原始的,对象目标,对象所有者);
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饶是如何在他的 NCommon 框架:
Have a look at how Ritesh Rao has done this in his NCommon framework:
的<一个href=\"http://github.com/riteshrao/ncommon/blob/master/NCommon.NHibernate/tests/Domain/MoneyUserType.cs\"相对=nofollow> MoneyUserType 实现所谓的基类<一href=\"http://github.com/riteshrao/ncommon/blob/master/NCommon.NHibernate/src/CompositeUserTypeBase.cs\"相对=nofollow> CompositeUserTypeBase
还有在评论中更多的细节,但总结:
There's more detail in the comments but to summarize:
- deepcopy的 - 应该返回持久状态的深层副本,在实体和集合停止
- 拆卸 - 变换对象到其重新presentation缓存(即协会必须缓存为标识符值)
- 装配 - 重建从缓存重新presentation 对象
<一个href=\"http://web.archive.org/web/20121019101713/http://$c$cinsanity.com/archive/2009/02/12/using-fluent-nhibernate-in-rhinestone-part-i.aspx\"相对=nofollow>它在很多解释这里更详细
这篇关于如何正确地贯彻执行IUserType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!