Wanting to create an Money object in my application, I first read Fowler's description in his PoEAA book and then looked at JP Boodhoo's implementation here. This post requires at least a look at his article.
Since I am using NHibernate on my app I wanted to be able to just use the following mapping in my classes taht would use the money object in them like so:
<property name="Rate" type="Data.NHibernateImpl.Types.MoneyCompositeUserType,Data">
<column name="Amount"></column>
<column name="Currency"></column>
</property>

Fortunately, there is a similar example for implementing a composite user type in teh Hibernate in Action book that is (of all things) a Money object. This is good since documentation is lame for NHibernate and the comments are vague when implementing the ICompositeUserType interface.
First, JP's class with a slight addition of Currency string to it:

publicstructMoney

{

privatereadonlyint cents;

privatereadonlystring _currency;

privatedouble roundedAmount;

public Money(double amount)

: this(amount, "USD")

{

}

public Money(double amount, string currency)

{

roundedAmount = RoundToNearestPenny(amount);

cents = ToPennies(roundedAmount);

_currency = currency;

}

privatestaticint ToPennies(double amount)

{

returnConvert.ToInt32(amount * 100);

}

privatestaticdouble RoundToNearestPenny(double amount)

{

double quotient = amount / .01;

int wholePart = (int)quotient;

decimal mantissa = ((decimal)quotient) - wholePart;

return mantissa >= .5m ? .01 * (wholePart + 1) : .01 * wholePart;

}

publicdouble Amount

{

get { return roundedAmount; }

}

publicstring Currency

{

get

{

return _currency;

}

}

publicint Cents

{

get { return cents; }

}

publicMoney Add(Money other)

{

returnnewMoney(this.Amount + other.Amount);

}

publicMoney MultiplyBy(double multiplicationFactor)

{

returnnewMoney(this.Amount * multiplicationFactor);

}

}

Now, I need to implement ICompositeUserType (in the NHibernate.Type namespace) and map the properties Amount and Currency of teh class so that Nhibernate knows how to deal with it:

publicclassMoneyCompositeUserType : ICompositeUserType

{

///<summary>

///

/// Get the value of a property

///

///</summary>

///

///<param name="component">an instance of class mapped by this "type"</param>

///<param name="property">The position index of the property.Corresponds to the column

/// index within the IDataReader row.

/// See <see cref="NullSafeGet"/> method for indices knowledge.

///</param>

///<returns>

///the property value

///</returns>

///

publicobject GetPropertyValue(object component, int property)

{

// 0 =Amount

// 1 = Currency

Money money = (Money) component;

if(property==0)

{

return money.Amount;

}

else

{

return money.Currency;

}

}

///<summary>

///

/// Set the value of a property

///

///</summary>

///

///<param name="component">an instance of class mapped by this "type"</param>

Going back and forth between the Hibernate in Action book and reading this blog helped me figure out what needed to happen.
The granularity of control NHibernate still affords you while taking care of so many lowlevel details is staggering. True, it takes some digging to understand how the extension points plugin because of the documentation problem (but it has helped me learn JAVA along teh way since there are tons of Hibernate examples), but once you get into how the code works it is a breeze!

It's strange that deep copy () creates a new instance of immutable object.

Also, double type is no way to go with financial operations(because you can't represent 0,1dec as a sum of 1/2, 1/4, 1/8... precisly ) so this code just won't work - it must be Decimal in C# or (BigDecimal in java) or something like that.p.s. sorry for my bad english as it's evening and it's not my mothertongue