Skip to content

Value Objects in C#: net10.0, validated Money, EF Core 10 complex types - #2250

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/91701-csharp-value-objects
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/91701-csharp-value-objects

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates the domain-driven design/ValueObjects sample in place for the revised Value Objects article. The folder keeps its path, so the article's GitHub link stays valid.

Changed:

  • Both projects retargeted from net7.0 to net10.0. ValueObjects becomes a console app (OutputType Exe) and references Microsoft.EntityFrameworkCore.Sqlite.
  • Money refuses a negative amount and stores the upper-case currency code, so usd and USD compare equal. Create() returns Result<Money>; the errors live in a new MoneyErrors class.
  • Result.cs takes the Error(Code, Description, Type) / Result / Result<TValue> shape, with a Validation error type.
  • Payment gets a private constructor for EF Core. TicketPriceProvider drops the .Value! null-forgiving operator.
  • Tests move from NUnit 3.13.3 to xUnit v3 with plain Assert. One equality test name is fixed.

Added:

  • Program.cs, which runs the article's examples in the article's order.
  • Persistence/PaymentsDbContext.cs, which maps Money as an EF Core 10 complex type (SQLite in memory, so no database file is written).
  • ValueObjects/Route.cs, a record with collection equality.
  • MoneyValidationUnitTest (3), PersistenceUnitTest (1) and RouteEqualityUnitTest (1). There are 10 tests in total.

The classic ValueObjects.sln is unchanged. Nothing outside this folder is touched.

Packages, re-queried on NuGet on 2026-09-27, the day this PR opened:

  • Microsoft.EntityFrameworkCore.Sqlite 10.0.12
  • Microsoft.NET.Test.Sdk 18.10.1
  • xunit.v3.mtp-off 4.0.1
  • xunit.runner.visualstudio 4.0.0

Checked locally on SDK 10.0.302, runtime 10.0.10:

  • dotnet build ValueObjects.sln --configuration Release: 0 warnings, 0 errors
  • dotnet test ValueObjects.sln --configuration Release --no-build --filter "FullyQualifiedName!~Live": 10 passed, 0 failed
  • dotnet run --project ValueObjects: prints the output blocks the article shows
  • dotnet list package --vulnerable --include-transitive: no vulnerable packages in either project

Please leave this open: it merges on the day the updated article is published.

Retarget both projects from net7.0 to net10.0 and move the tests from
NUnit to xUnit v3 (plain Assert). Money now refuses a negative amount and
stores the upper-case currency code, with the Result/Error shape and a
MoneyErrors class. Add Program.cs running the article's examples in order,
a PaymentsDbContext mapping Money as an EF Core 10 complex type on SQLite
in memory, a Route value object with collection equality, and tests for
validation, persistence and route equality (10 in total).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant