Trellis.Primitives.Generator 3.0.0-alpha.98

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

Trellis.Primitives.Generator

NuGet

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.Primitives package

How It Works

  1. Analyzes your code for partial classes inheriting from RequiredString<TSelf> or RequiredGuid<TSelf>
  2. Generates implementation code at compile-time
  3. Code appears in IntelliSense automatically
  4. 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

There are no supported framework assets in this package.

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