using System.Collections.Immutable; using Connected.Entities; using Connected.Entities.Storage; namespace Connected.Data.Update; internal class AggregatedCommandBuilder { public StorageOperation? Build(TEntity entity) { if (entity is not IEntity ie) throw new ArgumentException(nameof(entity)); switch (ie.State) { case State.New: return BuildInsert(ie); case State.Default: return BuildUpdate(ie); case State.Deleted: return BuildDelete(ie); default: throw new NotSupportedException(); } } public List Build(ImmutableArray entities) { var result = new List(); foreach (var entity in entities) result.Add(Build(entity)); return result; } private StorageOperation? BuildInsert(IEntity entity) { return new InsertCommandBuilder().Build(entity); } private StorageOperation? BuildUpdate(IEntity entity) { return new UpdateCommandBuilder().Build(entity); } private StorageOperation? BuildDelete(IEntity entity) { return new DeleteCommandBuilder().Build(entity); } }