Trellis.Primitives.Generator
3.0.0-alpha.98
dotnet add package Trellis.Primitives.Generator --version 3.0.0-alpha.98
NuGet\Install-Package Trellis.Primitives.Generator -Version 3.0.0-alpha.98
<PackageReference Include="Trellis.Primitives.Generator" Version="3.0.0-alpha.98" />
<PackageVersion Include="Trellis.Primitives.Generator" Version="3.0.0-alpha.98" />
<PackageReference Include="Trellis.Primitives.Generator" />
paket add Trellis.Primitives.Generator --version 3.0.0-alpha.98
#r "nuget: Trellis.Primitives.Generator, 3.0.0-alpha.98"
#:package Trellis.Primitives.Generator@3.0.0-alpha.98
#addin nuget:?package=Trellis.Primitives.Generator&version=3.0.0-alpha.98&prerelease
#tool nuget:?package=Trellis.Primitives.Generator&version=3.0.0-alpha.98&prerelease
Trellis.Primitives.Generator
Roslyn source generator for creating strongly-typed value objects with automatic validation and IParsable support.
What It Does
Automatically generates boilerplate code for classes inheriting from RequiredString or RequiredGuid:
// You write this:
public partial class OrderId : RequiredGuid
{
}
// Generator creates:
// - TryCreate(Guid?) method returning Result<OrderId>
// - TryCreate(string?) method returning Result<OrderId>
// - NewUniqueV4() method for new GUIDs
// - NewUniqueV7() method for time-ordered GUIDs
// - Parse(string, IFormatProvider?) method (IParsable)
// - TryParse(string?, IFormatProvider?, out OrderId) method (IParsable)
// - Explicit cast operator from Guid
// - Validation error messages
Installation
This package is included automatically when you install Trellis.Primitives:
dotnet add package Trellis.Primitives
Note: Both packages are required - the main package provides base classes, this generator creates the implementations.
Generated API
For RequiredString<TSelf> Classes
public partial class ProductName : RequiredString<ProductName>
{
}
// Generated members:
ProductName.TryCreate(string?) // Result<ProductName>
ProductName.Parse(string, IFormatProvider?) // ProductName (throws)
ProductName.TryParse(string?, IFormatProvider?, out...) // bool
(ProductName)"ABC" // Explicit cast
For RequiredGuid<TSelf> Classes
public partial class UserId : RequiredGuid<UserId>
{
}
// Generated members:
UserId.NewUniqueV4() // New random GUID
UserId.NewUniqueV7() // New time-ordered GUID
UserId.TryCreate(Guid?) // Result<UserId>
UserId.TryCreate(string?) // Result<UserId>
UserId.Parse(string, IFormatProvider?) // UserId (throws)
UserId.TryParse(string?, IFormatProvider?, out...) // bool
(UserId)Guid.NewGuid() // Explicit cast
Requirements
- .NET Standard 2.0 compatible (source generators must target netstandard2.0)
- C# 9.0+ for partial class support
- Requires
Trellis.Primitivespackage
How It Works
- Analyzes your code for partial classes inheriting from
RequiredString<TSelf>orRequiredGuid<TSelf> - Generates implementation code at compile-time
- Code appears in IntelliSense automatically
- No runtime reflection - all compile-time
Validation
Generated code includes automatic validation:
var result = ProductName.TryCreate("");
// Returns: Result.Failure("Product Name cannot be empty.")
var result = UserId.TryCreate(Guid.Empty);
// Returns: Result.Failure("User Id cannot be empty.")
Error messages use the class name (e.g., "Product Name" from "ProductName").
Source Code
This is a source generator - it runs at compile-time and generates C# code. The generated code is visible in:
- Visual Studio: Project → Dependencies → Analyzers → Trellis.Primitives.Generator
- Output:
obj/Debug/net10.0/generated/
Resources
License
MIT © Xavier John
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 |
|---|---|---|
| 3.0.0-alpha.98 | 0 | 3/3/2026 |
| 3.0.0-alpha.95 | 25 | 3/2/2026 |
| 3.0.0-alpha.94 | 30 | 3/2/2026 |
| 3.0.0-alpha.93 | 33 | 3/1/2026 |
| 3.0.0-alpha.92 | 45 | 2/28/2026 |
| 3.0.0-alpha.83 | 33 | 2/27/2026 |