// ========================================================================== // ActionDispatcher.cs // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex Group // All rights reserved. // ========================================================================== using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace Squidex.Infrastructure.Dispatching { public sealed class ActionDispatcher { private static readonly Dictionary> Handlers; static ActionDispatcher() { Handlers = typeof(TTarget) .GetMethods() .Where(Helper.HasRightName) .Where(Helper.HasRightParameters) .Select(ActionDispatcherFactory.CreateActionHandler) .ToDictionary(h => h.Item1, h => h.Item2); } public static bool Dispatch(TTarget target, TIn item) { Action handler; if (!Handlers.TryGetValue(item.GetType(), out handler)) { return false; } handler(target, item); return true; } } }