How to implement correctly IUserType?
Asked Answered
A

2

23

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?

Armington answered 14/10, 2009 at 8:44 Comment(0)
J
11

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

Jeraldjeraldine answered 16/10, 2009 at 11:26 Comment(4)
The link at then end leads to a very interesting article. Thanks at lot.Armington
@md1337 Replaced with web.archiveAllo
Fixed archive.org link: web.archive.org/web/20121019101713/http://...codeinsanity.com/…Palmation
The link to the web archive is for the wrong article - it's Part I but discussion about implementing IUserType is apparently in Part II, which I can't find.Craner
C
6

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

Cleaves answered 17/3, 2010 at 15:44 Comment(1)
The link is broken again but you can find it here: web.archive.org/web/20150214164507/http://blog.miraclespain.com/…Dishonest

© 2022 - 2024 — McMap. All rights reserved.