namespace zero.Stores;
public partial class StoreOperations : IStoreOperations
{
///
public virtual Task Empty(string flavorAlias = null) where T : ZeroIdEntity, ISupportsFlavors, new() => Empty(flavorAlias);
///
public virtual Task Empty(string flavorAlias = null)
where T : ZeroIdEntity, ISupportsFlavors, new()
where TFlavor : T, new()
{
// throw if this entity is not allowed to be created without a flavor
if (flavorAlias.IsNullOrEmpty() && !Flavors.CanUseWithoutFlavors())
{
string defaultFlavor = Flavors.DefaultFlavorFor();
if (defaultFlavor.IsNullOrEmpty())
{
throw new ArgumentException("Can not create instance of an entity which is configured to to be only used as a flavor", nameof(flavorAlias));
}
flavorAlias = defaultFlavor;
}
// return default instance if no flavor is required
if (flavorAlias.IsNullOrEmpty())
{
return Task.FromResult(new());
}
// try to load and construct a specific flavor
FlavorConfig config = Flavors.Get(flavorAlias);
TFlavor result = config?.Construct(config) as TFlavor;
if (result == null)
{
return Task.FromResult(default);
}
result.Flavor = flavorAlias;
return Task.FromResult(result);
}
}