This example shows how to model quota changes as mutations, keep the state immutable, and enforce policy limits before change is applied.
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.
The domain state is map of user IDs to quota values:
- each user has an integer quota
- quota changes are applied through mutations
- policies prevent quota values from moving outside allowed bounds
The example runs two scenarios:
- an emergency increase for selected users
- a scheduled monthly reset
- registering the mutation engine with strict options
- executing both single mutations and batch mutations
- validating mutation input before state changes occur
- enforcing maximum and minimum quota limits through policies
- collecting engine statistics after the scenarios finish
Program.cs- composition root and scenario runnerBillingQuotas.csproj- console project definitionState/QuotaState.cs- immutable quota stateMutations/IncreaseQuotaMutation.csMutations/DecreaseQuotaMutation.csMutations/ResetQuotaMutation.csPolicies/MaxQuotaPolicy.csPolicies/PreventNegativeQuotaPolicy.csScenarios/EmergencyIncreaseScenario.csScenarios/MonthlyResetScenario.cs
Program.cs wires the engine like this:
- create
ServiceCollection - register
ModularityKit.MutatorwithMutationEngineOptions.Strict - build the service provider
- resolve
IMutationEngine - register quota policies
- run the two scenarios
- print statistics from
GetStatisticsAsync()
The scenarios then create their own initial state, build mutations, and call the engine.
IncreaseQuotaMutation increments a user's quota.
- validates
UserId - validates that
Amountis positive - applies a new quota value
- emits
ChangeSetentry for the modified user quota
DecreaseQuotaMutation decrements a user's quota.
- validates
UserId - validates that
Amountis positive - rejects changes that would drop quota below zero
- emits
ChangeSetentry for the modified quota
ResetQuotaMutation sets the user's quota back to zero.
- validates
UserId - applies zero value
- emits change entry for the affected user
MaxQuotaPolicy blocks increases that would exceed the configured maximum.
This policy is useful for:
- capping emergency overrides
- constraining tenant limits
- showing how policy evaluation can inspect the concrete mutation type
PreventNegativeQuotaPolicy blocks quota decreases that would go below zero.
This policy is useful for:
- ensuring accounting integrity
- protecting against accidental underflow
- demonstrating simple deny decisions
EmergencyIncreaseScenario starts from state with multiple users and applies batch of increases.
It shows:
- batch execution
- per mutation policy decisions
- mixed success outcomes inside one batch
MonthlyResetScenario resets every user's quota in one batch.
It shows:
- system level mutation context
- batch generation from current state
- folding successful results back into final state
Program.csState/QuotaState.csMutations/IncreaseQuotaMutation.csMutations/DecreaseQuotaMutation.csPolicies/MaxQuotaPolicy.csPolicies/PreventNegativeQuotaPolicy.csScenarios/EmergencyIncreaseScenario.csScenarios/MonthlyResetScenario.cs
dotnet run --project Examples/Core/BillingQuotas/BillingQuotas.csprojThe sample prints:
- the emergency increase scenario
- the monthly reset scenario
- success or policy denial messages
- final quota values
- aggregate engine statistics
The exact numbers depend on the runtime and any policy thresholds you change.