Hi Pete, thanks for your help!

I cannot use your approach because both Money and Currency are *struct*s 
and those can't be inherited. But I came up with another solution though! 
Here's what I did:

I added a Currency property to my Account class and mapped it to a string 
column using a custom UserType. All the Money properties (Debit, Credit, 
Balance, etc.) I mapped as *decimal* (since the Money struct can be 
implicitly cast to a decimal value). Then I modified my Money properties to 
look like this:

public Money Debit {
get { return *new Money(*debit, currency*)*; }
set
{
if(*new Money(*debit, currency*)* != value)
{
debit = value;
OnPropertyChanged("Debit");
}
}
}

where *debit* is a private field of type Money, and *currency* is a private 
field of type Currency.

This solution works great; I have a single Currency column in DB and 
appropriate columns for each Money property in Account class! :)
Thank you for your answer again!

On Thursday, September 27, 2012 12:34:38 PM UTC+2, PeteA wrote:
>
> It sounds as if the fundamental problems is that the currency is really a 
> property of the Account, not the individual amounts.  Could you map the 
> Money type with a parent reference [to the account] then use the value from 
> the Account?  I've not thought it through very thoroughly so this idea 
> could be impractical, but something like:
>
>  
>
> class Account {
>
>                 Currency { get ; set; }
>
>                 AccountBalance XxxAmount { get; set; }
>
> }
>
>  
>
> class Money 
> {                                                                    // 
> Money has an amount and a currency
>
>                 Amount { get; set; }
>
>                 Currency { get; protected set; }                 // 
> Protected as currency can only be assigned at instantiation time
>
>                 protected Money() {}
>
>                 public Money(Currency myCurrency) { this.Currency = 
> myCurrency; }
>
> }
>
>  
>
> class AccountBalance : Money {                                 
>                                                 // AccountBalance is money 
> and inherits whatever functionality is appropriated e.g. Credit, Debit
>
>                 protected Account MyAccount { get; set; 
> }                                           // The owning account isn't 
> anybody else's business
>
>                 override Currency { get { return MyAccount.Currency; } 
> }              // but the currency is determined by the associated account
>
>                 protected AccountBalance() {}
>
>                 public AccountBalance(Account) {}
>
> }
>
>  
>
> Pete
>
>  
>
> *From:* [email protected] <javascript:> [mailto:
> [email protected] <javascript:>] *On Behalf Of *Alen Galinec
> *Sent:* 26 September 2012 18:43
> *To:* [email protected] <javascript:>
> *Subject:* [nhusers] Persisting many CompositeUserType instances in a 
> single entity
>
>  
>
> Hi people!
>
> I have created a custom CompositeUserType that maps two properties of a 
> complex domain object and it works fine. The problem is that I have many 
> instances of this type in another entity and each of these instances shares 
> the same value for one of its properties and I would like to persist them 
> to DB so that there's only one column for that property per table, and as 
> many columns for the other property as there are instances of that object. 
> Blaaaaaaah, that sentence is awkward! :D
>
> Here's the deal. I'm developing an accounting application. I have an 
> entity called Account where I used the Money type to deal with money 
> values, and I have many properties in Account that are typeof(Money).
>
> class Account {
>
> Money DebitAmount { get; set; }
> Money CreditAmount { get; set; }
> Money LastYearBalance { get; set; }
> Money OpeningBalance { get; set; }
> ... // other properties
>
> }
>
> In my mapping file I use my MoneyComposite type to map Money values to two 
> columns:
>
> 1. PropertyName_Value
> 2. PropertyName_Currency
>
>
> This works great but unfortunately it creates a pair of such columns for 
> every typeof(Money) property in Account, i.e. DebitAmount has 
> DebitAmountValue and DebitAmountCurrency; CreditAmount has ... well you get 
> the picture.
> Since it's only logical that an account can be bound to only one currency, 
> it would be better if it were possible to have only one Currency column in 
> the underlying table where that currency would be used for every Money 
> instance in the entity.
>
> I hope I've made my question clear enough. So if anyone can please help me 
> with this and tell me if there's a way to achieve this (preferably) without 
> separating Currency from Money, I would be most grateful!
>
> Thank you for your time!
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "nhusers" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/nhusers/-/MnFwQx3SJ1QJ.
> To post to this group, send email to [email protected] <javascript:>
> .
> To unsubscribe from this group, send email to 
> [email protected] <javascript:>.
> For more options, visit this group at 
> http://groups.google.com/group/nhusers?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/nhusers/-/hW0MSdXAz48J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to