Jacobi.ValueObject 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Jacobi.ValueObject --version 1.0.0
                    
NuGet\Install-Package Jacobi.ValueObject -Version 1.0.0
                    
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="Jacobi.ValueObject" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Jacobi.ValueObject" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Jacobi.ValueObject" />
                    
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 Jacobi.ValueObject --version 1.0.0
                    
#r "nuget: Jacobi.ValueObject, 1.0.0"
                    
#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 Jacobi.ValueObject@1.0.0
                    
#: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=Jacobi.ValueObject&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Jacobi.ValueObject&version=1.0.0
                    
Install as a Cake Tool

Value Object

Usage

using Jacobi.ValueObject;

Two syntax variations:

[ValueObject<Guid>]
public partial record struct ProductId;

[ValueObject(typeof(Guid))]
public partial record struct ProductId;

Minimal effort:

[ValueObject<Guid>]
public partial record struct ProductId;

...

var prodId = new ProductId(Guid.NewGuid());

Using Options to manage what code (support) is generated.

[ValueObject<Guid>(Options = ValueObjectOptions.Parsable)]
public partial record struct ProductId;

...

var prodId = ProductId.Parse("<guid>");

Implement validation by providing a static bool IsValid(<datatype> value) method. You determine the accessibility (public, internal, private).

[ValueObject<Guid>]
public partial record struct ProductId
{
    public static bool IsValid(Guid id) => id != Guid.Empty;
}

...

var prodId = new ProductId(Guid.Empty);   // <- will throw

Options

Option Description
Constructor Makes the value-constructor public. This option is default if none are specified.
ImpicitFrom Adds an implicit assignment operator that allows assigning the <datatype> value to a new instance of the ValueObject. Additionally an implementation for the IEquatable<datatype> interface will also be generated.
ImplicitAs Adds an implicit assignment operator that allows assigning the ValueObject to a <datatype> variable. Additionally an implementation for the IEquatable<datatype> interface will also be generated.
ExplicitFrom Adds a static factory method From that construct a new ValueObject instance from a specified <datatype> value.
ToString Overrides the record struct dotnet implementation to return the ValueObject.Value as string.
Comparable Implements the IComparable<ValueObject> interface to compare between ValueObject instances. If ImplicitFrom and/or ImplictAs options are also active, an implementation for IComparable<datatype> is also generated.
Parsable Implements the IParsable<ValueObject> and ISpanParsable<ValueObject> interfaces to provide Parse and TryParse methods. Note that this option cannot be used in combination with a <datatype> of string (System.String).

Exceptions

The Jacobi.ValueObject.ValueObjectException is throw in these circumstances.

  • The default (parameterless) constructor of the ValueObject is called.
  • The Value property is accessed while the instance of the ValueObject was not correctly initialized.
  • If the ValueObject implements the IsValid static method and the value fails the test.

Project File

To see the generated source files for the value objects in your .csproj project file:

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>

Compiler Errors

Code Error
VO001 You have declare a ValueObject in the global namespace. It is mandatory to declare your ValueObjects inside a namespace.
VO002 You did [ValueObject(null)] - It cannot work without a datatype.
VO003 You used the Parsable option on a ValueObject with the string/Systsem.String datatype.

Compiler errors caused by you not following the rules 😃

  • Do not specify a default constructor. So do NOT do this: public partial record struct ProductId()

Unsupported

  • Json Serialization (System.Text.Json or Newtonsoft.Json)
  • AspNet (TypeConvertor)
  • EFcore (ValueConvertor)

For now, you have to write these yourself.

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 was computed.  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

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.3.0 254 11/16/2025
1.2.0 151 11/8/2025
1.1.0 136 11/7/2025
1.0.0 168 11/7/2025