Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4083530
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:19:29+00:00 2026-05-20T18:19:29+00:00

I basically have a Money value type, which consists of Amount and Currency .

  • 0

I basically have a Money value type, which consists of Amount and Currency. I need to map multiple Money values into a table, which has multiple fields for amount, but only one currency. In order words, I have the table:

Currency     Amount1    Amount2
===============================
USD          20.00      45.00

Which needs to be mapped to a class with 2 Money values (which can logically never have different currencies):

class Record
{ 
    public Money Value1 { get; set; }
    public Money Value2 { get; set; }
}

struct Money 
{
    public decimal Amount { get; set; }
    public string Currency { get;set; }
} 

(Example is a bit simplified)

The table schema cannot be changed. I’m happy to implement an IUserType, if needed, but I cannot figure out how to access the Currency column for both values.

How do I map this using NHibernate ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:19:29+00:00Added an answer on May 20, 2026 at 6:19 pm

    It is not possible.

    If you create a Money UserType the purpose of the type is to combine two primitives into a new single data type. That data type is now a single atomic unit. Because the UserType is considered an atomic type both columns must be present for each Money value mapped. The rule NHibernate is enforcing is in fact semantically correct. You have two Money values. Therefore you must have two currencies – they cannot share one because a single Money type has a Currency and Amount and that is one atomic data unit now, it cannot be split up or shared.

    You want to at times treat currency as variable, and therefore needing its own column, and at other times as fixed so that multiple UserTypes can all share a column. Even if that were valid, which it is not based on how a Money is defined, how would NHibernate know what you wanted to do when? The system cannot just know what you want at any given time. You would now need to also save additional data with the Money type that specified how any given value was intended to be used.

    Bottom line, your columns as is cannot be mapped as a UserType. If you cannot change the schema, the only way you can do this is to map all three columns as normal primitives and then construct (and deconstruct) a Money type in your application code.

    And for what ends? I am really surprised that even someone like Fowler suggests this approach as some kind of “best practice” without really considering the actual details. The fact is most data is a set where the currency is dictated for a row by some often implicit or external factor such as country of origin or country where a business operates etc etc.

    The occasions where you might actually even need currency you often have so much other baggage that arises from multiple currencies such as current exchange rates etc that having the currency as part and parcel of a Money type is not even that helpful. It is a convenience, but for data which is very inconvenient to work with and cannot be made otherwise. Most of the time currency is fixed and usually can even be inferred. So a Money type in typical cases either cannot come close to what you really need – a conversion, or it is simply unnecessary information. With few exceptions the Money type becomes just a dingleberry on the ass of the application.

    Before you spend a lot of time trying to implement something for possibly little or no other reason than somehow people have begun to call this “best practice”, ask yourself are you ever even going to need to use it for anything?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically have a page which shows a processing screen which has been flushed
I basically have something like this: void Foo(Type ty) { var result = serializer.Deserialize<ty>(inputContent);
I basically have 7 select statements that I need to have the results output
I have a problem where i need a cascade on multiple foreign keys pointing
I have three tables like this CREATE TABLE `money`.`categories` ( `ID` bigint(20) unsigned NOT
i have a website which is basically a omegle copy ( text version only
I basically have the following flow: XML -> JSON -> Spring MVC -> jsp
I basically have three tables, posts, images and postimages (this simply contains the ids
I basically have a unix process running and it is doing some heavy processing
I basically have a server set up and I'm accepting new clients(UNIX) and i'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.