|
| 1 | +# DomainEvents Library - Project Context |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +**DomainEvents** is a .NET library that facilitates implementing **transactional domain events** within domain-driven design (DDD) bounded contexts. The library provides a pub/sub mechanism for raising and handling domain events to manage side effects across multiple aggregates while maintaining consistency. |
| 6 | + |
| 7 | +### Purpose |
| 8 | + |
| 9 | +- Enable explicit implementation of side effects triggered by domain changes |
| 10 | +- Support in-process, same-domain event handling |
| 11 | +- Ensure transactional consistency (all event-related operations succeed or all fail) |
| 12 | +- Provide a clean separation between event publishers and handlers |
| 13 | + |
| 14 | +### Core Components |
| 15 | + |
| 16 | +| Component | Description | |
| 17 | +|-----------|-------------| |
| 18 | +| `IDomainEvent` | Marker interface for defining domain events | |
| 19 | +| `IPublisher` | Interface for raising/publishing domain events | |
| 20 | +| `IHandler<T>` | Interface for implementing event handlers | |
| 21 | +| `IResolver` | Interface for resolving handlers for a given event type | |
| 22 | +| `Publisher` | Concrete implementation of `IPublisher` | |
| 23 | +| `Resolver` | Concrete implementation of `IResolver` | |
| 24 | + |
| 25 | +### Architecture |
| 26 | + |
| 27 | +``` |
| 28 | +┌─────────────┐ ┌──────────────┐ ┌─────────────────┐ |
| 29 | +│ Publisher │────▶│ Resolver │────▶│ IHandler<T>[] │ |
| 30 | +│ (IPublisher)│ │ (IResolver) │ │ (Event Handlers)│ |
| 31 | +└─────────────┘ └──────────────┘ └─────────────────┘ |
| 32 | +``` |
| 33 | + |
| 34 | +## Building and Running |
| 35 | + |
| 36 | +### Prerequisites |
| 37 | + |
| 38 | +- .NET SDK 9.0 or later (for building/testing) |
| 39 | +- The library targets multiple frameworks: `net462`, `netstandard2.0`, `netstandard2.1`, `net9.0`, `net10.0` |
| 40 | + |
| 41 | +### Build Commands |
| 42 | + |
| 43 | +```bash |
| 44 | +# Restore dependencies |
| 45 | +dotnet restore |
| 46 | + |
| 47 | +# Build the library (Release configuration) |
| 48 | +dotnet build --configuration Release |
| 49 | + |
| 50 | +# Run tests |
| 51 | +dotnet test --configuration Release --verbosity normal |
| 52 | + |
| 53 | +# Build with specific version |
| 54 | +dotnet build --configuration Release -p:PackageVersion=4.1.0 |
| 55 | +``` |
| 56 | + |
| 57 | +### Project Structure |
| 58 | + |
| 59 | +``` |
| 60 | +DomainEvents/ |
| 61 | +├── src/ |
| 62 | +│ └── DomainEvents/ # Main library source |
| 63 | +│ ├── IDomainEvent.cs # Event marker interface |
| 64 | +│ ├── IHandle.cs # Handler interface |
| 65 | +│ ├── IPublisher.cs # Publisher interface |
| 66 | +│ ├── IResolver.cs # Resolver interface |
| 67 | +│ └── Impl/ |
| 68 | +│ ├── Publisher.cs # Publisher implementation |
| 69 | +│ └── Resolver.cs # Resolver implementation |
| 70 | +├── test/ |
| 71 | +│ └── DomainEvents.Tests/ # NUnit test project |
| 72 | +│ ├── Events/ # Test event definitions |
| 73 | +│ ├── Handlers/ # Test handler implementations |
| 74 | +│ └── Run/ # Test cases |
| 75 | +└── .github/ |
| 76 | + └── workflows/ |
| 77 | + └── CI-Build.yml # GitHub Actions CI/CD |
| 78 | +``` |
| 79 | + |
| 80 | +## Development Conventions |
| 81 | + |
| 82 | +### Coding Style |
| 83 | + |
| 84 | +- **Nullable reference types**: Disabled (`Nullable>disable</Nullable>`) |
| 85 | +- **Implicit usings**: Disabled (`ImplicitUsings>disable</ImplicitUsings>`) |
| 86 | +- **Target frameworks**: Multi-targeted for broad compatibility |
| 87 | +- **Naming**: PascalCase for interfaces (`IPublisher`, `IHandler<T>`), classes follow standard .NET conventions |
| 88 | + |
| 89 | +### Testing Practices |
| 90 | + |
| 91 | +- **Framework**: NUnit 4.x |
| 92 | +- **Test adapter**: NUnit3TestAdapter |
| 93 | +- **Coverage**: coverlet.collector for code coverage |
| 94 | +- **Test structure**: Separate folders for Events, Handlers, and Run (test cases) |
| 95 | + |
| 96 | +### Versioning |
| 97 | + |
| 98 | +- Uses **Nerdbank.GitVersioning** for semantic versioning |
| 99 | +- Version configuration in `version.json` |
| 100 | +- Public releases from `master` branch only |
| 101 | +- Current version: **4.1.0** |
| 102 | + |
| 103 | +### CI/CD Workflow |
| 104 | + |
| 105 | +The GitHub Actions workflow (`.github/workflows/CI-Build.yml`) handles: |
| 106 | + |
| 107 | +1. **Linting**: Super-linter on PR events |
| 108 | +2. **Build (Beta)**: For non-release branches with auto-versioning |
| 109 | +3. **Build (Release)**: For `release/*` branches |
| 110 | +4. **Testing**: Runs on every build |
| 111 | +5. **Packaging**: Publishes to GitHub Packages |
| 112 | +6. **Release**: Publishes to NuGet.org for release branches |
| 113 | + |
| 114 | +### Package Information |
| 115 | + |
| 116 | +- **Package ID**: `Dormito.DomainEvents` |
| 117 | +- **Assembly Name**: `Dormito` |
| 118 | +- **Root Namespace**: `DomainEvents` |
| 119 | +- **License**: MIT License |
| 120 | +- **Repository**: https://github.com/CodeShayk/DomainEvents |
| 121 | + |
| 122 | +## Usage Pattern |
| 123 | + |
| 124 | +### 1. Define an Event |
| 125 | + |
| 126 | +```csharp |
| 127 | +public class CustomerCreated : IDomainEvent |
| 128 | +{ |
| 129 | + public string Name { get; set; } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### 2. Create a Handler |
| 134 | + |
| 135 | +```csharp |
| 136 | +public class CustomerCreatedHandler : IHandler<CustomerCreated> |
| 137 | +{ |
| 138 | + public Task HandleAsync(CustomerCreated @event) |
| 139 | + { |
| 140 | + Console.WriteLine($"Customer created: {@event.Name}"); |
| 141 | + return Task.CompletedTask; |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +### 3. Register with DI Container |
| 147 | + |
| 148 | +```csharp |
| 149 | +public void ConfigureServices(IServiceCollection services) |
| 150 | +{ |
| 151 | + services.AddTransient<IResolver>(sp => |
| 152 | + new Resolver(sp.GetServices<IHandler>())); |
| 153 | + services.AddTransient<IPublisher, Publisher>(); |
| 154 | + services.AddTransient<IHandler, CustomerCreatedHandler>(); |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +### 4. Publish Events |
| 159 | + |
| 160 | +```csharp |
| 161 | +public class OrderService |
| 162 | +{ |
| 163 | + private readonly IPublisher _publisher; |
| 164 | + |
| 165 | + public OrderService(IPublisher publisher) |
| 166 | + { |
| 167 | + _publisher = publisher; |
| 168 | + } |
| 169 | + |
| 170 | + public async Task CreateOrderAsync(Order order) |
| 171 | + { |
| 172 | + // ... create order logic |
| 173 | + var @event = new OrderCreated { OrderId = order.Id }; |
| 174 | + await _publisher.RaiseAsync(@event); |
| 175 | + } |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +## Key Files Reference |
| 180 | + |
| 181 | +| File | Purpose | |
| 182 | +|------|---------| |
| 183 | +| `README.md` | Library documentation and usage examples | |
| 184 | +| `DomainEvents.sln` | Visual Studio solution file | |
| 185 | +| `src/DomainEvents/DomainEvents.csproj` | Library project file with package metadata | |
| 186 | +| `test/DomainEvents.Tests/DomainEvents.Tests.csproj` | Test project configuration | |
| 187 | +| `.github/workflows/CI-Build.yml` | CI/CD pipeline definition | |
| 188 | +| `nuget.config` | NuGet package source configuration | |
| 189 | +| `version.json` | GitVersioning configuration | |
| 190 | +| `License.md` | MIT License | |
0 commit comments