📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

Why would you model Money as a complex type rather than with a value converter in EF Core?

Mid-Level Coding Intermediate

🎯 Key points

  • Value converter = one property ↔ one column
  • Two-part Money in one column loses SQL filtering, sorting and SUM on the amount
  • Complex type maps to typed columns in the owner's table (EF Core 8+)
  • Queries on Total.Amount translate to SQL
  • Value semantics: no key, compared by value, fits immutable records
  • Domain behaviour (currency checks) stays on the Money type
  • Converter is right for single-value objects (OrderId, Email)

⏱ 30-second answer

A value converter maps one property to one column. Money has two parts, amount and currency, so a converter can only store it in one column: either a string like "12.50 AUD" or by dropping the currency. Either way SQL cannot filter, sort or SUM on the amount, cannot enforce the currency with a column constraint, and the LINQ query has to compare the whole converted value. A complex type (ComplexProperty(o => o.Total), EF Core 8+) maps Money to two typed columns in the owner's table (Total_Amount decimal(18,2), Total_Currency char(3)), so Where(o => o.Total.Amount > 100) and Sum translate to SQL. It has value semantics (no key, no identity, compared by value), works with immutable records, and lets Money keep its own behaviour, such as refusing to add AUD to NZD. Use a converter for single-value objects like OrderId or Email.

🗣 Full answer (senior)

How I would answer this in an interview

"The deciding fact is how many columns the value needs. A value converter turns one CLR property into one column value. That's perfect for OrderId wrapping a Guid or Email wrapping a string. Money is two facts, an amount and a currency. With a converter I'd have to squash them into one column, a string or a JSON blob, and then the database can't do arithmetic on the amount: no WHERE Amount > 100, no SUM, no index on it, and no check constraint on the currency code.

A complex type is made for this. ComplexProperty(o => o.Total) maps Money to Total_Amount and Total_Currency in the Orders table, each properly typed. LINQ like orders.Where(o => o.Total.Amount > 100).Sum(o => o.Total.Amount) becomes plain SQL. And because a complex type has no key, EF Core treats it as a value: two orders can hold equal Money values, and replacing Total with a new record is the normal way to change it.

That also keeps the domain clean. Money is an immutable record with methods like Add that throw on mismatched currencies, and persistence doesn't leak into it."


public sealed record Money(decimal Amount, string Currency)
{
    public Money Add(Money other) => other.Currency == Currency
        ? this with { Amount = Amount + other.Amount }
        : throw new InvalidOperationException("Currency mismatch.");
}

modelBuilder.Entity<Order>().ComplexProperty(o => o.Total, m =>
{
    m.Property(x => x.Amount).HasPrecision(18, 2);
    m.Property(x => x.Currency).HasMaxLength(3).IsFixedLength().IsUnicode(false);
});
SELECT SUM([o].[Total_Amount]) FROM [Orders] AS [o] WHERE [o].[Total_Amount] > 100.0;

Choosing

Value converter Complex type
Columns One One per member
Query on parts No Yes
Best for OrderId, Email, enums as strings Money, Address, DateRange

Production example

An invoicing model stored totals as "123.45 AUD" strings; monthly revenue reports loaded [~400k] rows to sum in memory. Moving to a complex type let SQL do the SUM and cut the report from [40 s] to [under 1 s].

🔍 What the interviewer is checking

Checks the boundary between converters and complex types.

↩ Usually asked after

More in EF Core Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me