MStack.ValueObjects 1.0.2

dotnet add package MStack.ValueObjects --version 1.0.2
                    
NuGet\Install-Package MStack.ValueObjects -Version 1.0.2
                    
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="MStack.ValueObjects" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MStack.ValueObjects" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="MStack.ValueObjects" />
                    
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 MStack.ValueObjects --version 1.0.2
                    
#r "nuget: MStack.ValueObjects, 1.0.2"
                    
#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 MStack.ValueObjects@1.0.2
                    
#: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=MStack.ValueObjects&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=MStack.ValueObjects&version=1.0.2
                    
Install as a Cake Tool

MStack.ValueObjects

DDD value object base classes plus a curated set of common value objects (Email, Slug, Money, Cpf, Cnpj, Document, Cep, PhoneNumberBr). Construction returns Result<T> so failures compose naturally with the rest of your code.

NuGet License: MIT


Why

Problem What this package gives you
Re-implementing Equals / GetHashCode for every primitive wrapper ValueObject and SingleValueObject<T> base classes — define GetEqualityComponents and you're done.
Throwing exceptions during construction Each common VO returns Result<T> from MStack.ErrorHandling. Failures stay in the Result pipeline.
Re-writing CPF / CNPJ / CEP / Phone validation Production-tested implementations included.

Install

dotnet add package MStack.ValueObjects

This brings MStack.ErrorHandling along.

Quick start

Roll your own value object

using MStack.ErrorHandling;

namespace MyApp;

public sealed class Coordinates : ValueObject
{
    public double Latitude { get; }
    public double Longitude { get; }

    private Coordinates(double lat, double lng)
    {
        Latitude = lat;
        Longitude = lng;
    }

    public static Result<Coordinates> Create(double lat, double lng)
    {
        if (lat is < -90 or > 90)
        {
            return Error.Validation(nameof(Latitude), "Must be between -90 and 90.");
        }

        if (lng is < -180 or > 180)
        {
            return Error.Validation(nameof(Longitude), "Must be between -180 and 180.");
        }

        return new Coordinates(lat, lng);
    }

    protected override IEnumerable<object?> GetEqualityComponents()
    {
        yield return Latitude;
        yield return Longitude;
    }
}

Or use the included VOs

using MStack.ValueObjects;

var emailResult = Email.Create("user@example.com");
var cpfResult   = Cpf.Create("390.533.447-05");
var cnpjResult  = Cnpj.Create("11.222.333/0001-81");
var docResult   = Document.Create("390.533.447-05");   // CPF or CNPJ
var cepResult   = Cep.Create("01310-100");
var phoneResult = PhoneNumberBr.Create("(11) 91234-5678");
var moneyResult = Money.Create(99.90m, "BRL");
var slugResult  = Slug.Create("hello-world");

if (emailResult.IsFailure) { /* errors live in result.Errors */ }

Failures are categorised as ErrorCategory.Validation, so MStack.ErrorHandling will turn them into a 400 Bad Request ProblemDetails automatically when used in a Minimal API.


What ships

Type Namespace Notes
ValueObject MStack.ValueObjects Abstract base. Override GetEqualityComponents.
SingleValueObject<T> MStack.ValueObjects Helper for VOs wrapping one value. Provides Value, ToString(), implicit operator T, and GetEqualityComponents.
Email MStack.ValueObjects Trim + lowercase, RFC-shaped regex.
Slug MStack.ValueObjects Lowercase letters, digits and single hyphens.
Money MStack.ValueObjects Decimal + ISO 4217 currency. Add requires same currency.
Cpf MStack.ValueObjects Brazilian CPF, full check-digit validation.
Cnpj MStack.ValueObjects Brazilian CNPJ, full check-digit validation.
Document MStack.ValueObjects CPF or CNPJ — auto-detected by length.
Cep MStack.ValueObjects Brazilian postal code, 8 digits.
PhoneNumberBr MStack.ValueObjects Brazilian phone, 10 or 11 digits. Rejects toll-free (0800). Includes ToE164().

VOs that inherit from SingleValueObject<T> expose Value (raw digits/string), ToFormatted() where formatting makes sense, implicit conversion to the underlying type, and structural equality. Those inheriting directly from ValueObject expose their own domain-specific properties.


License

MIT

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.0.2 101 5/9/2026
1.0.1 97 5/9/2026
1.0.0 93 5/9/2026