Files
mixtape/zero.Core/Architecture/Blueprints/BlueprintService.cs
T

77 lines
1.9 KiB
C#
Raw Normal View History

2021-11-20 13:52:28 +01:00
namespace zero.Architecture;
2021-11-19 16:11:12 +01:00
public class BlueprintService : IBlueprintService
{
protected IZeroOptions Options { get; set; }
protected IReadOnlyCollection<Blueprint> Blueprints { get; set; }
public BlueprintService(IZeroOptions options)
{
Options = options;
2021-11-26 12:31:33 +01:00
Blueprints = Options.For<BlueprintOptions>();
2021-11-19 16:11:12 +01:00
}
/// <inheritdoc />
public bool IsEnabled<T>(T model) => IsEnabled(model.GetType());
/// <inheritdoc />
public bool IsEnabled<T>()=> IsEnabled(typeof(T));
/// <inheritdoc />
public bool IsEnabled(Type type) => Blueprints.Any(x => x.ContentType.IsAssignableFrom(type));
/// <inheritdoc />
public bool TryGetBlueprint<T>(T model, out Blueprint blueprint) => TryGetBlueprint(model.GetType(), out blueprint);
/// <inheritdoc />
public bool TryGetBlueprint<T>(out Blueprint blueprint) => TryGetBlueprint(typeof(T), out blueprint);
/// <inheritdoc />
public bool TryGetBlueprint(Type type, out Blueprint blueprint)
{
blueprint = Blueprints.FirstOrDefault(x => x.ContentType.IsAssignableFrom(type));
return blueprint != null;
}
2021-12-15 15:34:05 +01:00
/// <inheritdoc />
public Dictionary<Type, Blueprint> GetAllBlueprints()
{
return Blueprints.ToDictionary(x => x.ContentType, x => x);
}
2021-11-19 16:11:12 +01:00
}
public interface IBlueprintService
{
/// <summary>
/// Check whether blueprinting functionality is enabled for a certain entity
/// </summary>
bool IsEnabled<T>();
/// <summary>
/// Check whether blueprinting functionality is enabled for a certain entity
/// </summary>
bool IsEnabled<T>(T model);
bool IsEnabled(Type type);
bool TryGetBlueprint<T>(T model, out Blueprint blueprint);
bool TryGetBlueprint<T>(out Blueprint blueprint);
bool TryGetBlueprint(Type type, out Blueprint blueprint);
2021-12-15 15:34:05 +01:00
/// <summary>
/// Get all registered blueprint configurations
/// </summary>
Dictionary<Type, Blueprint> GetAllBlueprints();
2021-11-19 16:11:12 +01:00
}