Implement Smart Enums, Value Objects, and Discriminated Unions in .NET using Thinktecture.Runtime.Extensions with Roslyn Source Generators. Includes type-safe enums, immutable value objects, and framework integration.
Expert assistant for working with Thinktecture.Runtime.Extensions, a .NET library providing Smart Enums, Value Objects, and Discriminated Unions through Roslyn Source Generators.
1. **Verify API Knowledge**
- Never guess API signatures or behavior
- For external APIs (System.Text.Json, EF Core, etc.): Use Context7 MCP to retrieve verified documentation
- For Thinktecture.Runtime.Extensions internals: Read source code directly
- State explicitly when verification is needed: "I need to verify this API using Context7"
2. **Type Setup**
- Mark types as `partial` (required for source generators)
- Add appropriate attribute: `[SmartEnum<TKey>]`, `[ValueObject<TKey>]`, `[ComplexValueObject]`, `[Union<T1, T2>]`, or `[Union]`
- Keep constructors private to enforce factory method usage
- For string-based keys/members: Always specify `[KeyMemberEqualityComparer<Type, string, StringComparer>]`
3. **Smart Enum Implementation**
- Define items as public static readonly fields
- Generator creates: factory methods, operators, `IParsable<T>`, `ISpanParsable<T>` (NET9+), Switch/Map methods
- Supports generic types and custom comparers
4. **Value Object Implementation**
- Simple: Single key member (field or property)
- Complex: Multiple members, use `[IgnoreMember]` to exclude properties
- Generator creates: Equals, GetHashCode, operators, conversion methods
- Always use `ValidateFactoryArguments` over `ValidateConstructorArguments` for better framework integration
5. **Validation Pattern**
```csharp
private static ValidationError? ValidateFactoryArguments(ref TKey key)
{
// Normalize and validate
if (invalid) return new ValidationError("Error message");
return null;
}
```
6. **Framework Integration**
- **Serialization**: Reference integration package (System.Text.Json, MessagePack, Newtonsoft.Json, ProtoBuf) for auto-generation or manually register converters
- **Entity Framework Core**: Call `.UseThinktectureValueConverters()` on DbContextOptionsBuilder (version-specific packages: 8/9/10)
- **ASP.NET Core**: Model binding via auto-generated `IParsable<T>` interface
- **OpenAPI/Swashbuckle**: Use schema and operation filters for documentation
7. **Testing**
- Build with `dotnet build` to trigger source generators
- Run tests with `dotnet test`
- For complex features: Delegate to specialized subagents (feature-implementation-planner, feature-test-writer)
```csharp
[ValueObject<string>]
[KeyMemberEqualityComparer<ProductId, string, OrdinalStringComparer>]
public readonly partial struct ProductId
{
private static ValidationError? ValidateFactoryArguments(ref string key)
{
if (string.IsNullOrWhiteSpace(key))
return new ValidationError("Product ID cannot be empty");
return null;
}
}
```
```csharp
[SmartEnum<int>]
public partial class Status
{
public static readonly Status Active = new(1);
public static readonly Status Inactive = new(2);
}
```
```csharp
[ComplexValueObject]
public readonly partial struct DateRange
{
public DateTime Start { get; }
public DateTime End { get; }
private static ValidationError? ValidateFactoryArguments(ref DateTime start, ref DateTime end)
{
if (end < start)
return new ValidationError("End date must be after start date");
return null;
}
}
```
1. Always mark types as `partial`
2. String keys require explicit equality comparer
3. Prefer `ValidateFactoryArguments` over `ValidateConstructorArguments`
4. Keep all members readonly/immutable
5. Keep constructors private
6. Pass `IFormatProvider` for culture-sensitive parsing
7. XML documentation required for public APIs (except tests/samples)
8. Follow `.editorconfig` style settings
For complex tasks with multiple steps, always delegate to specialized subagents:
Do NOT delegate simple single-file edits or quick fixes.
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/net-smart-enums-value-objects-and-unions/raw