Dalion.ValueObjects
1.1.0
dotnet add package Dalion.ValueObjects --version 1.1.0
NuGet\Install-Package Dalion.ValueObjects -Version 1.1.0
<PackageReference Include="Dalion.ValueObjects" Version="1.1.0" />
<PackageVersion Include="Dalion.ValueObjects" Version="1.1.0" />
<PackageReference Include="Dalion.ValueObjects" />
paket add Dalion.ValueObjects --version 1.1.0
#r "nuget: Dalion.ValueObjects, 1.1.0"
#:package Dalion.ValueObjects@1.1.0
#addin nuget:?package=Dalion.ValueObjects&version=1.1.0
#tool nuget:?package=Dalion.ValueObjects&version=1.1.0
Dalion ValueObjects
A C# code generator to cure Primitive Obsession.
ValueObjects is a .NET Source Generator and analyzer. It turns your primitives (ints, decimals etc.) into value objects that represent domain concepts (CustomerId, AccountBalance, etc.).
All generated code is compatible with C# 11.0 or higher and .NET 8.0 or higher.
Installation and usage
Add the Dalion.ValueObjects NuGet package to your project.
<ItemGroup>
<PackageReference
Include="Dalion.ValueObjects"
Version="[1.1.0,)"
ReferenceOutputAssembly="false"
OutputItemType="Analyzer"
/>
</ItemGroup>
Then, create a readonly partial record struct and annotate it with the [ValueObject<T>] attribute:
using System;
[ValueObject<Guid>]
public readonly partial record struct CustomerId;
This enables the following for the type:
- Guarding against creation of uninitialized or invalid instances in code (based on validation rules you define)
- Ability to normalize the underlying primitive value upon creation
Stringrepresentation based on the underlying primitive valueSystem.Text.JsonSerialization and deserialization supportTypeConvertersupport (see Type Conversion in .NET)- Ability to check if an instance is initialized and valid, and to retrieve validation error messages
- Ability to define pre-set values as
public static readonlyfields (even if they are considered to be invalid) - Implementation of
IComparableandIComparable<T> - Implementation of
IFormattablefor formatting support - Implementation of
IParsable<T>for parsing support - Implementation of
ISpanParsable<T>for span parsing support, andIUtf8SpanParsable<T>for UTF8 span parsing support (if the underlying type supports it) - Generation of a static property
Empty(name is configurable) that represents the default value for the value object type
It makes it possible to enable the following for the type, using arguments in the attribute to configure behavior:
- Equality comparison based on the underlying primitive value (configurable to be case-sensitive or case-insensitive for
string-based value objects) - Equality operators (
==and!=) based on the underlying primitive value - Implicit conversion to and from the underlying primitive value
- Optional generation of helper extension methods for FluentValidation integration
- Optional generation of helper extension methods to facilitate creation (e.g.
32.Celsius())
This library also enables multiple roslyn analyzers that validate correct usage of the [ValueObject<T>] attribute and the annotated types.
You can examine the generated code in the generated sample code files. The behavior of the generated types is documented by unit tests.
Similarly, the behavior of the roslyn analyzers is documented by unit tests.
All generated code is compatible with C# 11.0 or higher and .NET 8.0 or higher.
See the Wiki for more detailed information, such as getting started, tutorials, and how-tos.
Motivation
Creating value objects is a repetitive task. This project aims to reduce the amount of boilerplate code that needs to be written, by generating it.
Inspired by the awesome Vogen project.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.0 | 203 | 12/19/2025 |
| 1.0.0 | 178 | 11/2/2025 |
| 1.0.0-beta05 | 118 | 11/1/2025 |
| 1.0.0-beta04 | 153 | 10/31/2025 |
| 1.0.0-beta03 | 193 | 10/21/2025 |