|
| 1 | +# Project Structure and Architecture |
| 2 | + |
| 3 | +## Repository Layout |
| 4 | +``` |
| 5 | +Thinktecture.Runtime.Extensions/ |
| 6 | +├── docs/ # Markdown documentation |
| 7 | +├── samples/ # Example projects demonstrating usage |
| 8 | +├── src/ # Source code |
| 9 | +│ ├── Thinktecture.Runtime.Extensions/ # Core library |
| 10 | +│ ├── Thinktecture.Runtime.Extensions.SourceGenerator/ # Source generators and analyzers |
| 11 | +│ ├── Thinktecture.Runtime.Extensions.Json/ # System.Text.Json integration |
| 12 | +│ ├── Thinktecture.Runtime.Extensions.Newtonsoft.Json/ # Newtonsoft.Json integration |
| 13 | +│ ├── Thinktecture.Runtime.Extensions.MessagePack/ # MessagePack integration |
| 14 | +│ ├── Thinktecture.Runtime.Extensions.ProtoBuf/ # ProtoBuf integration |
| 15 | +│ ├── Thinktecture.Runtime.Extensions.EntityFrameworkCore7/ # EF Core 7 integration |
| 16 | +│ ├── Thinktecture.Runtime.Extensions.EntityFrameworkCore8/ # EF Core 8 integration |
| 17 | +│ ├── Thinktecture.Runtime.Extensions.EntityFrameworkCore9/ # EF Core 9 integration |
| 18 | +│ ├── Thinktecture.Runtime.Extensions.AspNetCore/ # ASP.NET Core integration |
| 19 | +│ └── Thinktecture.Runtime.Extensions.Swashbuckle/ # OpenAPI integration |
| 20 | +├── test/ # Unit tests (mirrors src structure) |
| 21 | +└── test-results/ # Test output files |
| 22 | +``` |
| 23 | + |
| 24 | +## Core Components |
| 25 | + |
| 26 | +### Core Library (`Thinktecture.Runtime.Extensions`) |
| 27 | +- **Attributes**: `[ValueObject]`, `[SmartEnum]`, `[Union]` |
| 28 | +- **Base Interfaces**: `IValidationError`, `ISmartEnum`, `IComplexValueObject` |
| 29 | +- **Utility Types**: `ValidationError`, `Empty` collections, `SingleItem` |
| 30 | +- **Core Abstractions**: Foundation types for generated code |
| 31 | + |
| 32 | +### Source Generator (`Thinktecture.Runtime.Extensions.SourceGenerator`) |
| 33 | +- **Code Generation**: Creates boilerplate for Smart Enums, Value Objects, Unions |
| 34 | +- **Analyzers**: Compile-time diagnostics and warnings |
| 35 | +- **Code Fixes**: Automatic fixes for common issues |
| 36 | +- **Performance**: Reflection-free generated code |
| 37 | + |
| 38 | +### Integration Packages |
| 39 | +Each integration package follows the same pattern: |
| 40 | +- **Converters/Formatters**: Handle serialization/deserialization |
| 41 | +- **Extension Methods**: Register with DI containers or configure options |
| 42 | +- **Type Descriptors**: Enable framework-specific type conversion |
| 43 | + |
| 44 | +## Key Design Patterns |
| 45 | + |
| 46 | +### Source Generator Pattern |
| 47 | +- Types are marked with attributes (`[ValueObject]`, `[SmartEnum]`, `[Union]`) |
| 48 | +- Source generator creates `partial` implementations at compile-time |
| 49 | +- Generated code includes constructors, factory methods, equality, comparison |
| 50 | +- Developers implement validation and custom logic in user code |
| 51 | + |
| 52 | +### Validation Pattern |
| 53 | +```csharp |
| 54 | +// Preferred validation approach |
| 55 | +static partial void ValidateFactoryArguments(ref ValidationError? validationError, ref T value) |
| 56 | +{ |
| 57 | + // Return ValidationError for framework integration |
| 58 | + // Modify value by reference for normalization |
| 59 | +} |
| 60 | + |
| 61 | +// Legacy validation (avoid for new code) |
| 62 | +static partial void ValidateConstructorArguments(T value) |
| 63 | +{ |
| 64 | + // Can only throw exceptions |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Factory Method Pattern |
| 69 | +Generated types provide multiple creation methods: |
| 70 | +- `Create(T value)`: Throws on validation failure |
| 71 | +- `TryCreate(T value, out Type? result)`: Returns bool success |
| 72 | +- `Validate(T value, IFormatProvider?, out Type? result)`: Returns ValidationError |
| 73 | + |
| 74 | +### Pattern Matching Support |
| 75 | +- `Switch()`: Execute actions based on type/value (void return) |
| 76 | +- `Map<TResult>()`: Transform values based on type/value (typed return) |
| 77 | +- Both methods are exhaustive by design |
| 78 | + |
| 79 | +## Framework Integration Architecture |
| 80 | + |
| 81 | +### Serialization Strategy |
| 82 | +1. **Project Reference Approach** (Preferred): |
| 83 | + - Add integration package as project reference |
| 84 | + - Attributes automatically applied to types |
| 85 | + |
| 86 | +2. **Manual Registration**: |
| 87 | + - Register converter factories in Startup/Program.cs |
| 88 | + - More flexible but requires manual setup |
| 89 | + |
| 90 | +### Entity Framework Integration |
| 91 | +- Use `.UseThinktectureValueConverters()` extension method |
| 92 | +- Automatic value converter registration for all supported types |
| 93 | +- Support for complex value objects with multiple properties |
| 94 | + |
| 95 | +### ASP.NET Core Model Binding |
| 96 | +- Relies on `IParsable<T>` interface (generated automatically) |
| 97 | +- String-based types use `[ObjectFactory<string>]` for custom parsing |
| 98 | +- Supports binding from route, query, and body parameters |
0 commit comments