Refine is a source generator to help you design data types.
- avoid primitive obsession
- "make illegal states unrepresentable" - Yaron Minsky
dotnet add package Refine
dotnet add package Refine.Generators
Add the RefinedTypeAttribute
to a class and mark it as partial
.
using Refine;
[RefinedType(typeof(string))]
public partial class FullName;
using Refine;
[RefinedType(typeof(string))]
public partial class FullName
{
private static string Transform(string value) =>
value?.Trim() ?? "";
private static bool TryValidate(string value) =>
!string.IsNullOrEmpty(value);
}
string raw = "\tJames T. Kirk ";
Console.WriteLine($"Raw: '{raw}'");
var refined = FullName.Create(raw);
Console.WriteLine($"Refined: '{refined.Value}'");
Raw: ' James T. Kirk '
Refined: 'James T. Kirk'
var bad = FullName.Create(Environment.NewLine);
throws ArgumentException: Validation failed for the provided value.
- Look at the samples
- Inspect the generated code
- add
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
to your csproj - generated code will be under
obj/Debug/net8.0/generated/
- add
- More coming...