Files
mixtape/zero.Core/Stores/StoreOperations.Empty.cs
T

45 lines
1.3 KiB
C#
Raw Normal View History

2021-12-02 13:43:04 +01:00
namespace zero.Stores;
public partial class StoreOperations : IStoreOperations
{
/// <inheritdoc />
2021-12-03 14:45:49 +01:00
public virtual Task<T> Empty<T>(string flavorAlias = null) where T : ZeroIdEntity, ISupportsFlavors, new() => Empty<T, T>(flavorAlias);
2021-12-02 13:43:04 +01:00
/// <inheritdoc />
2021-12-03 14:45:49 +01:00
public virtual Task<TFlavor> Empty<T, TFlavor>(string flavorAlias = null)
2021-12-02 13:43:04 +01:00
where T : ZeroIdEntity, ISupportsFlavors, new()
2021-12-02 16:06:34 +01:00
where TFlavor : T, new()
2021-12-02 13:43:04 +01:00
{
2021-12-02 16:06:34 +01:00
// throw if this entity is not allowed to be created without a flavor
2021-12-03 14:45:49 +01:00
if (flavorAlias.IsNullOrEmpty() && !Flavors.CanUseWithoutFlavors<T>())
2021-12-02 16:06:34 +01:00
{
2021-12-03 14:45:49 +01:00
string defaultFlavor = Flavors.DefaultFlavorFor<T>();
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;
2021-12-02 16:06:34 +01:00
}
// return default instance if no flavor is required
2021-12-03 14:45:49 +01:00
if (flavorAlias.IsNullOrEmpty())
2021-12-02 16:06:34 +01:00
{
return Task.FromResult<TFlavor>(new());
}
// try to load and construct a specific flavor
2021-12-03 14:45:49 +01:00
FlavorConfig config = Flavors.Get<T>(flavorAlias);
2021-12-02 13:43:04 +01:00
TFlavor result = config?.Construct(config) as TFlavor;
if (result == null)
{
return Task.FromResult<TFlavor>(default);
}
2021-12-03 14:45:49 +01:00
result.Flavor = flavorAlias;
2021-12-02 13:43:04 +01:00
return Task.FromResult(result);
}
}