Vogen 8.0.4-beta.1

This is a prerelease version of Vogen.
dotnet add package Vogen --version 8.0.4-beta.1
                    
NuGet\Install-Package Vogen -Version 8.0.4-beta.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Vogen" Version="8.0.4-beta.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vogen" Version="8.0.4-beta.1" />
                    
Directory.Packages.props
<PackageReference Include="Vogen" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Vogen --version 8.0.4-beta.1
                    
#r "nuget: Vogen, 8.0.4-beta.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Vogen@8.0.4-beta.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Vogen&version=8.0.4-beta.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Vogen&version=8.0.4-beta.1&prerelease
                    
Install as a Cake Tool

Vogen - Value Object Generator

What is the package?

This is a semi-opinionated library which is a Source Generator to generate value objects. The main goal is that the value objects generated have almost the same speed and memory performance as using primitives.

The value objects wrap simple primitives such as int, string, double etc.

To get started, add this package, and add a type such as:

[ValueObject<int>]
public partial struct CustomerId 
{
}

You can now treat CustomerId as you would an int and there is very little performance difference between the two:

var id = CustomerId.From(42);

And your method signatures change from:

void HandleCustomer(int customerId)

to

void HandleCustomer(CustomerId customerId)

The Source Generator generates code for things like creating the object and for performing equality.

Value objects help combat Primitive Obsession. Primitive Obsession means being obsessed with primitives. It is a Code Smell that degrades the quality of software.

"Primitive Obsession is using primitive data types to represent domain ideas" #

Some examples:

  • instead of int age - we'd have Age age. Age might have validation that it couldn't be negative
  • instead of string postcode - we'd have Postcode postcode. Postcode might have validation on the format of the text

The opinions are expressed as:

  • A value object (VO) is constructed via a factory method named From, e.g. Age.From(12)
  • A VO is equatable (Age.From(12) == Age.From(12))
  • A VO, if validated, is validated with a private static method named Validate that returns a Validation result
  • Any validation that is not Validation.Ok results in a ValueObjectValidationException being thrown

Instead of

int customerId = 42;

... we'd have

var customerId = CustomerId.From(42);

CustomerId is declared as:

[ValueObject<int>]
public partial struct CustomerId 
{
}

That's all you need to do to switch from a primitive to a value object.

Here it is again with some validation

[ValueObject<int>]
public partial struct CustomerId 
{
    private static Validation Validate(int value) => 
        value > 0 ? Validation.Ok : Validation.Invalid(); 
}

This allows us to have more strongly typed domain objects instead of primitives, which makes the code easier to read and enforces better method signatures, so instead of:

void DoSomething(int customerId, int supplierId, int amount)

we can have:

void DoSomething(CustomerId customerId, SupplierId supplierId, Amount amount)

Now, callers can't mess up the ordering of parameters and accidentally pass us a Supplier ID in place of a Customer ID.

It also means that validation is in just one place. You can't introduce bad objects into your domain, therefore you can assume that in your domain every ValueObject is valid.

Adding the package

Add the package to your application using

dotnet add package Vogen

This adds a <PackageReference> to your project.

Unlike using some other source generators, specifying PrivateAssets="all" ExcludeAssets="runtime" with Vogen will cause exceptions at runtime because of missing types.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
  </PropertyGroup>

  
  <PackageReference Include="Vogen" Version="8.0.2" />
  

</Project>

How does it compare to using native types?

Here's the benchmarks comparing a native int to a ValueObject:

Method Mean Error StdDev Ratio Allocated
UsingIntNatively 17.04 ns 0.253 ns 0.014 ns 1.00 -
UsingValueObjectStruct 19.76 ns 2.463 ns 0.135 ns 1.16 -

There's hardly any speed overhead, and no memory overhead.

The next most common scenario is using a VO to represent a string:

Method Mean Error StdDev Ratio Allocated
UsingStringNatively 204.4 ns 8.09 ns 0.44 ns 1.00 256 B
UsingValueObjectAsClass 250.7 ns 29.97 ns 1.64 ns 1.23 328 B
UsingValueObjectAsStruct 248.9 ns 18.82 ns 1.03 ns 1.22 304 B
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (12)

Showing the top 5 NuGet packages that depend on Vogen:

Package Downloads
PiBox.Hosting.Abstractions

PiBox is a `service hosting framework` that allows `.net devs` to `decorate their services with behaviours or functionality (think of plugins) while only using minimal configuration`.

Codehard.FileService.Contracts

An abstract contract for file service client and server.

Dosaic.Hosting.Abstractions

A plugin-first dotnet framework for rapidly building anything hosted in the web.

Vogen.Serialization

Provides serialization support between Vogen and Newtonsoft.Json

HarvestForecast.Client

HarvestForecast.Client is an unofficial client library for the HarvestForecast API. Just add an access token and get started straight away!

GitHub repositories (8)

Showing the top 8 popular GitHub repositories that depend on Vogen:

Repository Stars
ardalis/CleanArchitecture
Clean Architecture Solution Template: A proven Clean Architecture Template for ASP.NET Core 10
thomhurst/TUnit
A modern, fast and flexible .NET testing framework
JasperFx/marten
.NET Transactional Document DB and Event Store on PostgreSQL
SSWConsulting/SSW.VerticalSliceArchitecture
An enterprise ready solution template for Vertical Slice Architecture. This template is just one way to apply the Vertical Slice Architecture.
SSWConsulting/SSW.CleanArchitecture
SSW Clean Architecture Template
SteveDunn/PacManBlazor
PACMAN in Blazor WebAssembly
AArnott/Nerdbank.MessagePack
A feature-packed .NET MessagePack serialization library with great performance and simplicity. msgpack.io[C#]
Hona/VerticalSliceArchitecture
Spend less time over-engineering, and more time coding. The template has a focus on convenience, and developer confidence. Vertical Slice Architecture 🎈
Version Downloads Last Updated
8.0.4-beta.1 58 11/30/2025
8.0.3 8,136 11/24/2025
8.0.3-beta.2 386 10/6/2025
8.0.3-beta.1 63 10/4/2025
8.0.2 84,834 9/17/2025
8.0.1 108,595 8/12/2025
8.0.0-beta.2 454 6/25/2025
8.0.0-beta.1 5,296 6/6/2025
7.0.5-beta.1 135 6/5/2025
7.0.4 333,578 6/5/2025
7.0.4-beta.1 186 5/22/2025
7.0.3 342,071 3/25/2025
7.0.2 48,974 3/17/2025
7.0.1 119,946 2/25/2025
7.0.0 48,236 2/13/2025
7.0.0-beta.1 409 1/9/2025
6.0.0 263,725 12/28/2024
6.0.0-beta.4 126 12/27/2024
6.0.0-beta.3 1,160 12/26/2024
6.0.0-beta.2 172 12/3/2024
6.0.0-beta.1 109 12/2/2024
5.0.6 91,930 11/30/2024
5.0.6-beta.3 184 11/23/2024
5.0.6-beta.2 96 11/22/2024
5.0.6-beta.1 106 11/19/2024
5.0.5 51,835 11/14/2024
5.0.5-rc.2 155 11/2/2024
5.0.5-rc.1 119 11/1/2024
5.0.5-beta.3 124 10/31/2024
5.0.5-beta.2 123 10/27/2024
5.0.4-beta.1 137 10/21/2024
5.0.3 108,615 9/26/2024
5.0.2 2,077 9/25/2024
5.0.1 1,438 9/23/2024
5.0.0-beta.1 414 9/9/2024
4.0.19 26,673 8/21/2024
4.0.18 1,468 8/17/2024
4.0.17 19,508 8/6/2024
4.0.16 2,768 7/30/2024
4.0.15 98,116 7/20/2024
4.0.14 10,548 7/16/2024
4.0.13 2,993 7/13/2024
4.0.12 19,524 6/21/2024
4.0.11 845 6/20/2024
4.0.10 951 6/19/2024
4.0.9 1,555 6/18/2024
4.0.8 34,798 5/30/2024
4.0.7 812 5/29/2024
4.0.6 8,301 5/27/2024
4.0.5 639 5/26/2024 4.0.5 is deprecated because it has critical bugs.
4.0.4 8,800 5/11/2024
4.0.3 3,169 5/8/2024
4.0.2 7,874 4/30/2024
4.0.1 612 4/29/2024
4.0.0 13,805 4/19/2024
3.0.25-beta.1 1,716 2/20/2024
3.0.24 420,038 1/9/2024
3.0.23 560,626 11/3/2023
3.0.23-beta.2 110 11/1/2023
3.0.23-beta.1 112 11/1/2023
3.0.22 13,443 10/23/2023
3.0.21 198,209 8/31/2023
3.0.20 232,303 7/17/2023
3.0.19 37,554 6/5/2023
3.0.18 10,780 5/30/2023
3.0.17 418 5/29/2023
3.0.16 13,055 5/9/2023
3.0.15 42,936 4/14/2023
3.0.14 2,069 4/13/2023
3.0.13 3,145 3/29/2023
3.0.12 69,163 1/30/2023
3.0.11 4,217 1/24/2023
3.0.10 652 1/14/2023
3.0.9 4,493 12/29/2022
3.0.8 495 12/27/2022
3.0.7 2,417 12/16/2022
3.0.6 584 12/10/2022
3.0.5 796 11/26/2022
3.0.4 12,309 11/7/2022
3.0.3 629 10/30/2022
3.0.2-alpha.2 238 10/12/2022
3.0.2-alpha 231 10/11/2022
3.0.1 782 10/8/2022
2.0.5 1,063 9/29/2022
2.0.4 573 9/28/2022
2.0.3 4,658 7/27/2022
2.0.2 596 7/27/2022
1.0.25 1,298 7/25/2022
1.0.24 621 7/24/2022
1.0.23 662 7/21/2022
1.0.22 1,739 6/18/2022
1.0.21 2,122 5/13/2022
1.0.20 709 5/3/2022
1.0.19 681 4/25/2022
1.0.18 13,543 2/21/2022
1.0.17 650 2/6/2022
1.0.16-alpha.1 289 1/29/2022
1.0.15 1,540 1/2/2022
1.0.12 476 12/28/2021
1.0.11 466 12/28/2021
1.0.9 2,554 12/11/2021
1.0.8 508 12/6/2021
1.0.7 480 12/5/2021
1.0.6 1,042 12/3/2021
1.0.5 493 12/2/2021
1.0.4 445 12/2/2021
1.0.3 491 12/1/2021
1.0.2 468 12/1/2021
1.0.1 2,623 12/1/2021
1.0.1-alpha.0.2 296 12/1/2021
0.0.0-alpha.0 279 1/2/2022