Skip to content
Pawel Gerr edited this page Mar 4, 2023 · 53 revisions

Build TestResults

Thinktecture.Runtime.Extensions
Thinktecture.Runtime.Extensions.EntityFrameworkCore
Thinktecture.Runtime.Extensions.EntityFrameworkCore5
Thinktecture.Runtime.Extensions.EntityFrameworkCore6
Thinktecture.Runtime.Extensions.Json
Thinktecture.Runtime.Extensions.Newtonsoft.Json
Thinktecture.Runtime.Extensions.MessagePack
Thinktecture.Runtime.Extensions.AspNetCore

This library provides some interfaces, classes, Roslyn Source Generators, Roslyn Analyzers and Roslyn CodeFixes for implementation of Smart Enums and Value Objects.

Requirements

  • Version 6:
    • C# 11 (or higher) for generated code
    • SDK 7.0.102 (or higher) for building projects
  • Version 5:
    • C# 9 (or higher) for generated code
    • SDK 6.0.300 (or higher) for building projects

Smart Enums

Install: Install-Package Thinktecture.Runtime.Extensions

Documentation: Smart Enums

Features:

  • Roslyn Analyzers and CodeFixes help the developers to implement the Smart Enums correctly
  • Allows iteration over all items
  • Allows custom properties and methods
  • Provides appropriate constructor based on the specified properties/fields
  • Provides means for lookup, cast and type conversion from key-type to Smart Enum and vice versa
  • Provides proper implementation of Equals, GetHashCode, ToString and equality comparison via == and !=
  • Choice between always-valid IEnum<T> and maybe-valid IValidatableEnum<T>
  • Allows custom validation of constructor arguments
  • Allows changing the property name Key, which holds the underlying value - thanks to Roslyn Source Generator
  • Allows custom key comparer
  • JSON support (System.Text.Json and Newtonsoft.Json)
  • ASP.NET Core support (model binding and model validation)
  • Entity Framework Core support (ValueConverter)
  • MessagePack support (IMessagePackFormatter)

Definition of a new Smart Enum without any custom properties and methods. All other features mentioned above are generated by the Roslyn Source Generators in the background.

public partial class ProductType : IEnum<string>
{
   public static readonly ProductType Groceries = new("Groceries");
   public static readonly ProductType Housewares = new("Housewares");
}

Definition of a new Smart Enum with 1 custom property RequiresFoodVendorLicense and 1 method Do with different behaviors for different enum items.

public partial class ProductType : IEnum<string>
{
   public static readonly ProductType Groceries = new("Groceries",  requiresFoodVendorLicense: true);
   public static readonly ProductType Housewares = new HousewaresProductType();

   public bool RequiresFoodVendorLicense { get; }

   public virtual void Do()
   {
      // do default stuff
   }

   private class HousewaresProductType : ProductType
   {
      public HousewaresProductType()
         : base("Housewares", requiresFoodVendorLicense: false)
      {
      }

      public override void Do()
      {
         // do special stuff
      }
   }
}

Value Objects

Install: Install-Package Thinktecture.Runtime.Extensions

Documentation: Immutable Value Objects

Features:

  • Roslyn Analyzers and CodeFixes help the developers to implement the Value Objects correctly
  • Allows custom properties and methods
  • Provides appropriate factory methods for creation of new value objects based on the specified properties/fields
  • Allows custom validation of constructor and factory method arguments
  • Additional features for simple Value Objects (1 "key"-property/field) and complex Value Objects (2 properties/fields or more)
  • Simple Value Objects: allows cast and type conversion from key-type to Value Object and vice versa
  • Simple Value Objects: provides an implementation of IComparable<T> if the key-property/field is an IComparable<T> or has an IComparer<T>
  • Simple Value Objects: provides an implementation of IFormattable if the key-property/field is an IFormattable
  • Provides proper implementation of Equals, GetHashCode, ToString and equality comparison via == and !=
  • Allows custom equality comparison
  • JSON support (System.Text.Json and Newtonsoft.Json)
  • ASP.NET Core support (model binding and model validation)
  • Entity Framework Core support (ValueConverter)
  • MessagePack support (IMessagePackFormatter)

Definition of a value object with 1 custom property Value. All other features mentioned above are generated by the Roslyn Source Generators in the background.

[ValueObject]
public partial class ProductName
{
   public string Value { get; }

   // The member can be a private readoly field as well
   //private readonly string _value;
}

Definition of a complex value object with 2 properties and a custom validation of the arguments.

[ValueObject]
public partial class Boundary
{
   public decimal Lower { get; }
   public decimal Upper { get; }

   static partial void ValidateFactoryArguments(ref ValidationResult? validationResult, ref decimal lower, ref decimal upper)
   {
      if (lower <= upper)
         return;

      validationResult = new ValidationResult($"Lower boundary '{lower}' must be less than upper boundary '{upper}'");
   }
}