|
| 1 | +# BillingQuotas |
| 2 | + |
| 3 | +This example shows how to model quota changes as mutations, keep the state immutable, and enforce policy limits before change is applied. |
| 4 | + |
| 5 | +It is the most compact sample in the repository. Use it when you want to see the engine doing one thing well: controlled numeric state changes with validation and policy checks. |
| 6 | + |
| 7 | +## Domain |
| 8 | + |
| 9 | +The domain state is map of user IDs to quota values: |
| 10 | + |
| 11 | +- each user has an integer quota |
| 12 | +- quota changes are applied through mutations |
| 13 | +- policies prevent quota values from moving outside allowed bounds |
| 14 | + |
| 15 | +The example runs two scenarios: |
| 16 | + |
| 17 | +- an emergency increase for selected users |
| 18 | +- a scheduled monthly reset |
| 19 | + |
| 20 | +## What this example demonstrates |
| 21 | + |
| 22 | +- registering the mutation engine with strict options |
| 23 | +- executing both single mutations and batch mutations |
| 24 | +- validating mutation input before state changes occur |
| 25 | +- enforcing maximum and minimum quota limits through policies |
| 26 | +- collecting engine statistics after the scenarios finish |
| 27 | + |
| 28 | +## Project structure |
| 29 | + |
| 30 | +- [`Program.cs`](Program.cs) - composition root and scenario runner |
| 31 | +- [`BillingQuotas.csproj`](BillingQuotas.csproj) - console project definition |
| 32 | +- [`State/QuotaState.cs`](State/QuotaState.cs) - immutable quota state |
| 33 | +- [`Mutations/IncreaseQuotaMutation.cs`](Mutations/IncreaseQuotaMutation.cs) |
| 34 | +- [`Mutations/DecreaseQuotaMutation.cs`](Mutations/DecreaseQuotaMutation.cs) |
| 35 | +- [`Mutations/ResetQuotaMutation.cs`](Mutations/ResetQuotaMutation.cs) |
| 36 | +- [`Policies/MaxQuotaPolicy.cs`](Policies/MaxQuotaPolicy.cs) |
| 37 | +- [`Policies/PreventNegativeQuotaPolicy.cs`](Policies/PreventNegativeQuotaPolicy.cs) |
| 38 | +- [`Scenarios/EmergencyIncreaseScenario.cs`](Scenarios/EmergencyIncreaseScenario.cs) |
| 39 | +- [`Scenarios/MonthlyResetScenario.cs`](Scenarios/MonthlyResetScenario.cs) |
| 40 | + |
| 41 | +## How it works |
| 42 | + |
| 43 | +`Program.cs` wires the engine like this: |
| 44 | + |
| 45 | +1. create `ServiceCollection` |
| 46 | +2. register `ModularityKit.Mutator` with `MutationEngineOptions.Strict` |
| 47 | +3. build the service provider |
| 48 | +4. resolve `IMutationEngine` |
| 49 | +5. register quota policies |
| 50 | +6. run the two scenarios |
| 51 | +7. print statistics from `GetStatisticsAsync()` |
| 52 | + |
| 53 | +The scenarios then create their own initial state, build mutations, and call the engine. |
| 54 | + |
| 55 | +## Mutation flow |
| 56 | + |
| 57 | +### Increase quota |
| 58 | + |
| 59 | +[`IncreaseQuotaMutation`](Mutations/IncreaseQuotaMutation.cs) increments a user's quota. |
| 60 | + |
| 61 | +- validates `UserId` |
| 62 | +- validates that `Amount` is positive |
| 63 | +- applies a new quota value |
| 64 | +- emits `ChangeSet` entry for the modified user quota |
| 65 | + |
| 66 | +### Decrease quota |
| 67 | + |
| 68 | +[`DecreaseQuotaMutation`](Mutations/DecreaseQuotaMutation.cs) decrements a user's quota. |
| 69 | + |
| 70 | +- validates `UserId` |
| 71 | +- validates that `Amount` is positive |
| 72 | +- rejects changes that would drop quota below zero |
| 73 | +- emits `ChangeSet` entry for the modified quota |
| 74 | + |
| 75 | +### Reset quota |
| 76 | + |
| 77 | +[`ResetQuotaMutation`](Mutations/ResetQuotaMutation.cs) sets the user's quota back to zero. |
| 78 | + |
| 79 | +- validates `UserId` |
| 80 | +- applies zero value |
| 81 | +- emits change entry for the affected user |
| 82 | + |
| 83 | +## Policies |
| 84 | + |
| 85 | +### Max quota |
| 86 | + |
| 87 | +[`MaxQuotaPolicy`](Policies/MaxQuotaPolicy.cs) blocks increases that would exceed the configured maximum. |
| 88 | + |
| 89 | +This policy is useful for: |
| 90 | + |
| 91 | +- capping emergency overrides |
| 92 | +- constraining tenant limits |
| 93 | +- showing how policy evaluation can inspect the concrete mutation type |
| 94 | + |
| 95 | +### Prevent negative quota |
| 96 | + |
| 97 | +[`PreventNegativeQuotaPolicy`](Policies/PreventNegativeQuotaPolicy.cs) blocks quota decreases that would go below zero. |
| 98 | + |
| 99 | +This policy is useful for: |
| 100 | + |
| 101 | +- ensuring accounting integrity |
| 102 | +- protecting against accidental underflow |
| 103 | +- demonstrating simple deny decisions |
| 104 | + |
| 105 | +## Scenarios |
| 106 | + |
| 107 | +### Emergency increase |
| 108 | + |
| 109 | +[`EmergencyIncreaseScenario`](Scenarios/EmergencyIncreaseScenario.cs) starts from state with multiple users and applies batch of increases. |
| 110 | + |
| 111 | +It shows: |
| 112 | + |
| 113 | +- batch execution |
| 114 | +- per mutation policy decisions |
| 115 | +- mixed success outcomes inside one batch |
| 116 | + |
| 117 | +### Monthly reset |
| 118 | + |
| 119 | +[`MonthlyResetScenario`](Scenarios/MonthlyResetScenario.cs) resets every user's quota in one batch. |
| 120 | + |
| 121 | +It shows: |
| 122 | + |
| 123 | +- system level mutation context |
| 124 | +- batch generation from current state |
| 125 | +- folding successful results back into final state |
| 126 | + |
| 127 | +## What to read first |
| 128 | + |
| 129 | +1. [`Program.cs`](Program.cs) |
| 130 | +2. [`State/QuotaState.cs`](State/QuotaState.cs) |
| 131 | +3. [`Mutations/IncreaseQuotaMutation.cs`](Mutations/IncreaseQuotaMutation.cs) |
| 132 | +4. [`Mutations/DecreaseQuotaMutation.cs`](Mutations/DecreaseQuotaMutation.cs) |
| 133 | +5. [`Policies/MaxQuotaPolicy.cs`](Policies/MaxQuotaPolicy.cs) |
| 134 | +6. [`Policies/PreventNegativeQuotaPolicy.cs`](Policies/PreventNegativeQuotaPolicy.cs) |
| 135 | +7. [`Scenarios/EmergencyIncreaseScenario.cs`](Scenarios/EmergencyIncreaseScenario.cs) |
| 136 | +8. [`Scenarios/MonthlyResetScenario.cs`](Scenarios/MonthlyResetScenario.cs) |
| 137 | + |
| 138 | +## Run |
| 139 | + |
| 140 | +```bash |
| 141 | +dotnet run --project Examples/BillingQuotas/BillingQuotas.csproj |
| 142 | +``` |
| 143 | + |
| 144 | +## Expected output |
| 145 | + |
| 146 | +The sample prints: |
| 147 | + |
| 148 | +- the emergency increase scenario |
| 149 | +- the monthly reset scenario |
| 150 | +- success or policy denial messages |
| 151 | +- final quota values |
| 152 | +- aggregate engine statistics |
| 153 | + |
| 154 | +The exact numbers depend on the runtime and any policy thresholds you change. |
| 155 | + |
0 commit comments