Jacobi.ValueObject
1.0.0
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
<PackageReference Include="Jacobi.ValueObject" Version="1.0.0" />
<PackageVersion Include="Jacobi.ValueObject" Version="1.0.0" />
<PackageReference Include="Jacobi.ValueObject" />
paket add Jacobi.ValueObject --version 1.0.0
#r "nuget: Jacobi.ValueObject, 1.0.0"
#:package Jacobi.ValueObject@1.0.0
#addin nuget:?package=Jacobi.ValueObject&version=1.0.0
#tool nuget:?package=Jacobi.ValueObject&version=1.0.0
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
Valueproperty is accessed while the instance of the ValueObject was not correctly initialized. - If the ValueObject implements the
IsValidstatic 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 | Versions 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. |
-
.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.