Strong Typing

The StringlyTyped project advocates for the use of strong typing through Value Objects to combat the code smell of “StringlyTyped” code, which relies heavily on primitive data types (like strings) to represent domain concepts.

Value Objects

Value Objects are immutable, strongly typed objects that represent a specific domain concept. They offer a more robust and expressive way to model data than using primitives.

Benefits of using Value Objects:

  • Improved Clarity: Value Objects encapsulate domain logic, making code easier to understand and reason about.
  • Reduced Errors: Validation and constraints are enforced at the Value Object level, preventing invalid data from entering the system.
  • Increased Maintainability: Changes to domain logic only need to be made in one place: the Value Object definition.

Example:

// Instead of:
          int customerId = 42;
          
          // Use:
          var customerId = CustomerId.From(42);
          

Implementation:

The StringlyTyped library provides a base ValueObject class. This class offers the following features:

  • Validation: Derived classes must implement the Validate() method to enforce data integrity.
  • Immutability: Value Objects are immutable, ensuring that their state remains consistent.
  • Equality: The Equals method is overridden to compare Value Objects based on their internal values.
  • Static Factory Methods: Value Objects are created using the static From method, ensuring validation and immutability are enforced.

Example:

public class PlayerNumber : ValueObject<int>
          {
              public override Validation Validate() => 
                  Value is >=1 and <=12  ? Validation.Ok : Validation.Invalid("Player number mst be between 1 and 12.");
          }
          

Validation:

Validation is implemented through the Validation class. This class provides a mechanism for indicating whether a Value Object is valid and capturing error messages.

Example:

public class Validation
          {
              // ...
              public static Validation Invalid(string reason = "")
              {
                  if (string.IsNullOrEmpty(reason))
                  {
                      return new Validation("[none provided]");
                  }
          
                  return new Validation(reason);
              }
          }
          

Key Features:

  • Immutable: Value Objects are immutable, preventing accidental modification of their state.
  • Strongly Typed: Value Objects use specific types to represent domain concepts, enforcing data integrity and providing better code readability.
  • Validation: The Validate() method allows you to enforce business rules and ensure that Value Objects are created in a valid state.
  • Equality: The Equals method provides a consistent and predictable way to compare Value Objects.

Using StringlyTyped:

  1. Install the NuGet Package: Install the StringlyTyped package from NuGet. https://github.com/SteveDunn/StringlyTyped
  2. Create Value Objects: Define your Value Objects as classes inheriting from the ValueObject class.
  3. Implement Validation: Implement the Validate() method in your Value Objects to enforce specific business rules.

By adopting strong typing with Value Objects, you can build more robust, maintainable, and error-free software.


          ## Top-Level Directory Explanations
          
          <a class='local-link directory-link' data-ref="samples/" href="#samples/">samples/</a> - This directory contains example projects demonstrating the usage of StringlyTyped library.
          
          <a class='local-link directory-link' data-ref="src/" href="#src/">src/</a> - This directory contains the source code of the StringlyTyped library.
          
          <a class='local-link directory-link' data-ref="tests/" href="#tests/">tests/</a> - This directory contains unit tests for the StringlyTyped library. It includes benchmark tests and small tests.