Project · Production companions
Check conditional mutation and compatible receipts
Run twelve boundary checks for one-owner versions, identity reuse and current receipts.
All C# source files below are loaded together in the playground. Files written by a run live in its virtual directory.
Program.cs
VersionChecks.Run();
Domain/VersionPolicy.cs
public sealed class VersionPolicy {
private readonly Dictionary<string,(int Quantity,int Expected,int Result)> receipts=new(StringComparer.Ordinal);
public int Version {get;private set;}=1;
public int ReceiptCount=>receipts.Count;
public string Apply(string key,int quantity,int expected,bool weak=false) {
if(string.IsNullOrWhiteSpace(key)||quantity is <1 or >10)return "invalid";
if(weak)return "stale";
if(receipts.TryGetValue(key,out var prior))return prior.Quantity==quantity&&prior.Expected==expected&&prior.Result==Version?"replay":"conflict";
if(expected!=Version)return "stale";
Version++; receipts.Add(key,(quantity,expected,Version)); return "created";
}
}
Checks/VersionChecks.cs
public static class VersionChecks {
public static void Run() {
var owner=new VersionPolicy();int checks=0;void Check(bool value){if(!value)throw new Exception("Independent version expectation failed");checks++;}
Check(owner.Apply("one",2,1)=="created");
Check(owner.Version==2&&owner.ReceiptCount==1);
Check(owner.Apply("one",2,1)=="replay");
Check(owner.Apply("one",3,1)=="conflict");
Check(owner.Apply("one",2,2)=="conflict");
Check(owner.Apply("two",2,1)=="stale");
Check(owner.Apply("two",2,2,true)=="stale");
Check(owner.Apply("two",3,2)=="created");
Check(owner.Version==3&&owner.ReceiptCount==2);
Check(owner.Apply("one",2,1)=="conflict");
Check(owner.Apply("",2,3)=="invalid");
Check(owner.Version==3&&owner.ReceiptCount==2);
Console.WriteLine("version checks:"+checks);Console.WriteLine("owner:memory only");
}
}
VersionPolicy.csproj
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net9.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable></PropertyGroup></Project>