using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using zero.Core.Entities; using zero.Core.Options; namespace zero.Core.Blueprints { public class BlueprintService : IBlueprintService { protected IZeroOptions Options { get; set; } protected IReadOnlyCollection Blueprints { get; set; } public BlueprintService(IZeroOptions options) { Options = options; Blueprints = Options.Blueprints.GetAllItems(); } /// public bool IsEnabled(T model) where T : ZeroEntity => IsEnabled(model.GetType()); /// public bool IsEnabled() where T : ZeroEntity => IsEnabled(typeof(T)); /// public bool IsEnabled(Type type) => Blueprints.Any(x => x.ContentType.IsAssignableFrom(type)); /// public bool TryGetBlueprint(T model, out Blueprint blueprint) where T : ZeroEntity => TryGetBlueprint(model.GetType(), out blueprint); /// public bool TryGetBlueprint(out Blueprint blueprint) where T : ZeroEntity => TryGetBlueprint(typeof(T), out blueprint); /// public bool TryGetBlueprint(Type type, out Blueprint blueprint) { blueprint = Blueprints.FirstOrDefault(x => x.ContentType.IsAssignableFrom(type)); return blueprint != null; } } public interface IBlueprintService { /// /// Check whether blueprinting functionality is enabled for a certain entity /// bool IsEnabled() where T : ZeroEntity; /// /// Check whether blueprinting functionality is enabled for a certain entity /// bool IsEnabled(T model) where T : ZeroEntity; bool IsEnabled(Type type); bool TryGetBlueprint(T model, out Blueprint blueprint) where T : ZeroEntity; bool TryGetBlueprint(out Blueprint blueprint) where T : ZeroEntity; bool TryGetBlueprint(Type type, out Blueprint blueprint); } }