Trellis.Primitives 3.0.0-alpha.100

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

Primitive Value Objects

NuGet Package

Infrastructure and ready-to-use implementations for primitive value objects with source code generation, eliminating boilerplate code and primitive obsession in domain-driven design applications.

Installation

Install both packages via NuGet:

dotnet add package Trellis.Primitives
dotnet add package Trellis.Primitives.Generator

Important: Both packages are required:

  • Trellis.Primitives — Base classes and 12 ready-to-use value objects
  • Trellis.Primitives.Generator — Source generator for Required* derivatives

Quick Start

RequiredString

Create strongly-typed string value objects using source code generation:

public partial class TrackingId : RequiredString<TrackingId>
{
}

// The source generator automatically creates:
// - TryCreate(string?, string? fieldName = null) -> Result<TrackingId>
// - Parse / TryParse (IParsable<T>)
// - explicit operator TrackingId(string)

var result = TrackingId.TryCreate("TRK-12345");
if (result.IsSuccess)
    Console.WriteLine(result.Value); // TRK-12345

// With custom field name for validation errors
var result2 = TrackingId.TryCreate(input, "shipment.trackingId");

Optional: enforce length constraints with [StringLength]:

[StringLength(50)]
public partial class FirstName : RequiredString<FirstName> { }

[StringLength(500, MinimumLength = 10)]
public partial class Description : RequiredString<Description> { }

RequiredGuid

Use NewUniqueV7() for time-ordered, sortable identifiers — GUID V7 provides the same benefits as ULIDs (sequential, timestamp-embedded) with the standard System.Guid type.

public partial class EmployeeId : RequiredGuid<EmployeeId>
{
}

var employeeId = EmployeeId.NewUniqueV7(); // Time-ordered, sortable
var result = EmployeeId.TryCreate(guid);
var result2 = EmployeeId.TryCreate("550e8400-e29b-41d4-a716-446655440000");

RequiredInt and RequiredDecimal

public partial class Quantity : RequiredInt<Quantity> { }
public partial class Price : RequiredDecimal<Price> { }

var qty = Quantity.TryCreate(10);
var price = Price.TryCreate(99.99m);

Ready-to-Use Value Objects

Value Object Purpose Validation Example
EmailAddress Email validation RFC 5322 compliant user@example.com
Url Web URLs Absolute HTTP/HTTPS https://example.com
PhoneNumber Phone numbers E.164 format +14155551234
Percentage Percentage values 0–100 range 15.5 or 15.5%
CurrencyCode Currency codes ISO 4217 3-letter USD, EUR
IpAddress IP addresses IPv4 and IPv6 192.168.1.1
Hostname Hostnames RFC 1123 example.com
Slug URL slugs Lowercase, digits, hyphens my-blog-post
CountryCode Country codes ISO 3166-1 alpha-2 US, GB
LanguageCode Language codes ISO 639-1 alpha-2 en, es
Age Age values 0–150 range 42
Money Monetary amounts Non-negative + ISO 4217 currency 99.99 USD
var email = EmailAddress.TryCreate("user@example.com");
var url = Url.TryCreate("https://example.com/path");
var phone = PhoneNumber.TryCreate("+14155551234");
var pct = Percentage.TryCreate(15.5m);
var money = Money.TryCreate(99.99m, "USD");

RequiredEnum

Type-safe enumerations that replace C# enums with full-featured classes:

public partial class OrderState : RequiredEnum<OrderState>
{
    public static readonly OrderState Draft = new();
    public static readonly OrderState Confirmed = new();
    public static readonly OrderState Shipped = new();
}

var result = OrderState.TryCreate("Confirmed");
// result.Value == OrderState.Confirmed

ASP.NET Core Integration

Value objects implementing IScalarValue work seamlessly with ASP.NET Core:

// 1. Register in Program.cs
builder.Services
    .AddControllers()
    .AddScalarValueValidation();

// 2. Use in DTOs
public record CreateUserDto
{
    public FirstName FirstName { get; init; } = null!;
    public EmailAddress Email { get; init; } = null!;
    public Maybe<Url> Website { get; init; } // Optional — null → Maybe.None
}

// 3. Controllers get automatic validation
[HttpPost]
public IActionResult Create(CreateUserDto dto)
{
    // All value objects validated on deserialization!
    return Ok(dto);
}

See Trellis.Asp for full ASP.NET Core integration.

Generated Code Features

  • TryCreate methods returning Result<T> with optional fieldName parameter
  • IParsable<T> implementation (Parse/TryParse)
  • Explicit cast operators
  • Property name inference for error messages (class name → camelCase)
  • JSON serialization via ParsableJsonConverter<T>
  • OpenTelemetry activity tracing support

Best Practices

  1. Use partial classes — Required for source code generation
  2. Leverage generated methods — Use TryCreate for safe parsing
  3. Use the fieldName parameter — Better validation error messages in APIs
  4. Prefer specific types over primitivesEmployeeId is more expressive than Guid
  5. Use meaningful names — Class name becomes part of error messages

License

MIT — see LICENSE for details.

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 (1)

Showing the top 1 NuGet packages that depend on Trellis.Primitives:

Package Downloads
Trellis.EntityFrameworkCore

EF Core integration for Trellis. Convention-based value converter registration for Trellis primitives, Result-returning SaveChanges wrappers, Maybe/Result query extensions, and provider-agnostic database exception classification.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0-alpha.100 24 3/4/2026
3.0.0-alpha.99 31 3/4/2026
3.0.0-alpha.98 34 3/3/2026
3.0.0-alpha.95 40 3/2/2026
3.0.0-alpha.94 42 3/2/2026
3.0.0-alpha.93 42 3/1/2026
3.0.0-alpha.92 59 2/28/2026
3.0.0-alpha.83 42 2/27/2026