public static Protobuf.Winner3Way FromDomain(this Winner3Way v) { var target = new Protobuf.Winner3Way(); switch (v.Win1) { case Outcome.Empty c: target.Win1Empty = c.FromDomain(); break; case Outcome.Priced c: target.Win1Priced = c.FromDomain(); break; case Outcome.PricedWithProb c: target.Win1PricedWithProb = c.FromDomain(); break; case Outcome.Resulted c: target.Win1Resulted = c.FromDomain(); break; } switch (v.Draw) { case Outcome.Empty c: target.DrawEmpty = c.FromDomain(); break; case Outcome.Priced c: target.DrawPriced = c.FromDomain(); break; case Outcome.PricedWithProb c: target.DrawPricedWithProb = c.FromDomain(); break; case Outcome.Resulted c: target.DrawResulted = c.FromDomain(); break; } switch (v.Win2) { case Outcome.Empty c: target.Win2Empty = c.FromDomain(); break; case Outcome.Priced c: target.Win2Priced = c.FromDomain(); break; case Outcome.PricedWithProb c: target.Win2PricedWithProb = c.FromDomain(); break; case Outcome.Resulted c: target.Win2Resulted = c.FromDomain(); break; } return(target); }
static void Main(string[] args) { var m1 = new Winner3Way( new Outcome.Priced(1.85m), new Outcome.PricedWithProb(3.3m, 0.33f), new Outcome.Resulted(OutcomeResult.Canceled)); Console.WriteLine($"Orig: {m1}"); using var outputStream = new MemoryStream(); using var output = new Google.Protobuf.CodedOutputStream(outputStream); m1.FromDomain().WriteTo(output); output.Flush(); var bytes = outputStream.ToArray(); Console.WriteLine($"Protobuf Bytes Length={bytes.Length}, {BitConverter.ToString(bytes)}"); using var inputStream = new MemoryStream(bytes); using var input = new Google.Protobuf.CodedInputStream(inputStream); var protoCopy = new Domain.Protobuf.Winner3Way(); protoCopy.MergeFrom(input); var copy = protoCopy.ToDomain(); Console.WriteLine($"Copy: {copy}"); using var jsonStream = new MemoryStream(); using var jsonWriter = new System.Text.Json.Utf8JsonWriter(jsonStream); m1.WriteJson(jsonWriter); jsonWriter.Flush(); var jsonString = Encoding.UTF8.GetString(jsonStream.ToArray()); Console.WriteLine($"Json {jsonString}"); var jsonDoc = System.Text.Json.JsonDocument.Parse(jsonString); var jsonCopy = Json.ReadWinner3Way(jsonDoc.RootElement); Console.WriteLine($"Json Copy: {jsonCopy}"); ComplexDomainDemo.Run(); }
public static void Run() { var s1 = new Score(5, 0); Console.WriteLine($"ID: {s1.GetId()}"); var m1 = new Winner3Way( new Outcome.Priced(1.85m), new Outcome.Priced(3.3m), new Outcome.Priced(2.0m)); var outcomes = m1.GetOutcomes(); Console.WriteLine($"Outcomes: {string.Join(",", outcomes)}"); var newOutcomes = outcomes.Select(pair => (pair.Item1, pair.Item2 switch { Outcome.Priced c => c with { Price = c.Price * 1.1m },
public static Outcome GetOutcome(this Winner3Way x, string idx) => idx switch { "(1)" => x.Win1,