ClassValidator Basics
Overview
Section titled “Overview”A newly joined team member declared a domain Value Object as internal and committed it without sealed. Compilation passes without issues, but this class cannot be accessed from other layers and is exposed to unintended inheritance. Should you leave the same review comment every time?
A class’s visibility and modifiers are expressions of design intent. Let tests — not humans — protect that intent.
In this part, you will learn the core verification methods of ClassValidator. You will step by step learn how to enforce class visibility, modifiers, naming, and inheritance/interface implementation through architecture tests.
Learning Objectives
Section titled “Learning Objectives”Core Learning Goals
Section titled “Core Learning Goals”- Architecture test environment setup
- Load assemblies to automatically collect types for verification
- Understand the roles of
ArchRuleDefinitionandValidateAllClasses
- Enforcing visibility and modifier rules
- Protect design intent with
RequirePublic,RequireSealed,RequireAbstract, etc. - Verify C#-specific modifiers with
RequireRecord,RequireStatic, etc.
- Protect design intent with
- Automating naming rules
- Verify name rules based on suffixes, prefixes, and regular expressions through tests
- Verifying inheritance hierarchies and interface implementations
- Enforce type relationships with
RequireInherits,RequireImplements
- Enforce type relationships with
What You Will Verify Through Practice
Section titled “What You Will Verify Through Practice”- Automatically verify that domain classes are
public sealed - Enforce
Specsuffix naming rules - Check whether classes inherit from a specific abstract class or implement a specific interface
Chapter Structure
Section titled “Chapter Structure”| Ch | Title | Key Content |
|---|---|---|
| Ch 1 | First Architecture Test | ArchLoader, ValidateAllClasses, RequirePublic, RequireSealed |
| Ch 2 | Visibility and Modifiers | RequireInternal, RequireAbstract, RequireStatic, RequireRecord |
| Ch 3 | Naming Rules | RequireNameEndsWith, RequireNameStartsWith, RequireNameMatching |
| Ch 4 | Inheritance and Interfaces | RequireInherits, RequireImplements, RequireImplementsGenericInterface |
In the first chapter, you will load an assembly with ArchLoader and write your first architecture test using RequirePublic and RequireSealed.