Skip to content
/ refine Public

Type refinement using Roslyn source generation

License

Notifications You must be signed in to change notification settings

jltrem/refine

Repository files navigation

Refine

Refine is a source generator to help you design data types.

Quick Start

1. Add the NuGet

dotnet add package Refine 
dotnet add package Refine.Generators

2. Decorate Your Class

Add the RefinedTypeAttribute to a class and mark it as partial.

using Refine;

[RefinedType(typeof(string))]
public partial class FullName;

3. Transform and/or Validate

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);
}

4. Instantiate

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'

5. Invalid States Are Unrepresentable

var bad = FullName.Create(Environment.NewLine);

throws ArgumentException: Validation failed for the provided value.

Deeper Dive

  1. Look at the samples
  2. Inspect the generated code
    • add <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> to your csproj
    • generated code will be under obj/Debug/net8.0/generated/
  3. More coming...

About

Type refinement using Roslyn source generation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages