Validation

Vogen uses validation to ensure the constraints of a value object are met. This is accomplished through the Validate method.

Example:

[ValueObject]
          public partial struct CustomerId
          {
              private static Validation Validate(int value) =>
              value > 0 ? Validation.Ok : Validation.Invalid("Customer IDs must be greater than 0.");
          }
          

How it works:

  1. Validate method: A private, static method named Validate is used for validation.
  2. Validation type: The Validate method returns a Validation result.
  3. Validation.Ok: Indicates successful validation.
  4. Validation.Invalid: Indicates failed validation.
  5. ValueObjectValidationException: If the validation fails (Validation.Invalid), a ValueObjectValidationException is thrown.

Example (from ValidationTutorial.md):

var newCustomer = CustomerId.From(0);
          

Output:

Unhandled exception. Vogen.ValueObjectValidationException:
          Customer IDs must be greater than 0.
          

Important Considerations:

  • Validate Method’s Purpose: The Validate method is private and static because it’s not meant to be called directly by users. If a value object has a CustomerId, it’s already considered valid within the domain.
  • Pre-set Instances: You can define pre-set instances for special scenarios (like “unspecified”). See the tutorial on Pre-set Values.
  • Validation in One Place: This approach centralizes validation, preventing the introduction of invalid values into your domain.
  • Domain-Specific Validation: Validation rules are based on your domain logic. You can enforce specific constraints based on your application’s requirements.

Why Use Validation?

  • Prevent Invalid Values: Prevents the creation of invalid objects, ensuring data integrity.
  • Strong Typing: Enforces domain rules and improves code readability.
  • Error Handling: Provides clear error messages to guide debugging.

References:

Top-Level Directory Explanations

samples/ - This directory contains example projects that demonstrate the usage of the Vogen library. Each subdirectory represents a different example, and contains the necessary files and configurations for that example.

src/ - This directory contains the source code for the project. It includes the Vogen library itself, as well as any shared types and code fixers.

src/obj/ - This directory contains object files generated during the compilation process.

src/Vogen/ - This subdirectory contains the core Vogen library code. It includes subdirectories for diagnostics, extensions, generators, properties, rules, suppressors, templates, and binaries and object files.

tests/ - This directory contains unit tests and benchmarks for the project. It includes subdirectories for analyzer tests, consumer tests, snapshot tests, and Vogen benchmarks.

tests/AnalyzerTests/ - This subdirectory contains unit tests for the analyzer component of the Vogen library.

tests/ConsumerTests/ - This subdirectory contains unit tests for the consumer-side components of the Vogen library.

tests/SnapshotTests/ - This subdirectory contains snapshot tests, which test the output of the code generation and serialization components of the Vogen library.

tests/Vogen.Benchmarks/ - This subdirectory contains benchmarks for the performance of the Vogen library.