Trellis.Primitives
3.0.0-alpha.100
dotnet add package Trellis.Primitives --version 3.0.0-alpha.100
NuGet\Install-Package Trellis.Primitives -Version 3.0.0-alpha.100
<PackageReference Include="Trellis.Primitives" Version="3.0.0-alpha.100" />
<PackageVersion Include="Trellis.Primitives" Version="3.0.0-alpha.100" />
<PackageReference Include="Trellis.Primitives" />
paket add Trellis.Primitives --version 3.0.0-alpha.100
#r "nuget: Trellis.Primitives, 3.0.0-alpha.100"
#:package Trellis.Primitives@3.0.0-alpha.100
#addin nuget:?package=Trellis.Primitives&version=3.0.0-alpha.100&prerelease
#tool nuget:?package=Trellis.Primitives&version=3.0.0-alpha.100&prerelease
Primitive Value Objects
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 objectsTrellis.Primitives.Generator— Source generator forRequired*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
TryCreatemethods returningResult<T>with optionalfieldNameparameterIParsable<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
- Use partial classes — Required for source code generation
- Leverage generated methods — Use
TryCreatefor safe parsing - Use the fieldName parameter — Better validation error messages in APIs
- Prefer specific types over primitives —
EmployeeIdis more expressive thanGuid - Use meaningful names — Class name becomes part of error messages
Related Packages
- Trellis.Primitives.Generator — Source generator (required companion)
- Trellis.Results — Core
Result<T>type - Trellis.DomainDrivenDesign — Entity and aggregate patterns
- Trellis.Asp — ASP.NET Core integration
License
MIT — see LICENSE for details.
| Product | Versions 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. |
-
net10.0
- Trellis.DomainDrivenDesign (>= 3.0.0-alpha.100)
- Trellis.Results (>= 3.0.0-alpha.100)
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 |