You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Data/Update/AggregatedCommandBuilder.cs

52 lines
1.1 KiB

using System.Collections.Immutable;
using Connected.Entities;
using Connected.Entities.Storage;
namespace Connected.Data.Update;
internal class AggregatedCommandBuilder<TEntity>
{
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<StorageOperation> Build(ImmutableArray<TEntity> entities)
{
var result = new List<StorageOperation>();
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);
}
}