using System.Collections.Generic; using JD.Kernel.Classes; using JD.Kernel.StaticHelpers; namespace JD.TeaBoxAddinSystem.Addins { public class ActionHandlerRepository where THandler : class { #region Variables private readonly Dictionary> m_handlers = new Dictionary>(); #endregion #region Constructor public ActionHandlerRepository(string extensionPath) { Throw.IfEmpty(extensionPath, "extensionPath"); Init(extensionPath); } /// /// Ctor for unit test purpose. /// protected ActionHandlerRepository() { } #endregion #region Methods protected void Init(string extensionPath) { System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(extensionPath)); foreach (HandlerExtensionNode node in GetExtensionNodes(extensionPath)) { if (!string.IsNullOrEmpty(node.ActionId)) { if (!m_handlers.ContainsKey(node.ActionId)) { m_handlers.Add(node.ActionId, node.Handler); } else { LogService.LogService.LogError("More than one action handler registered with action {0}.", node.ActionId); } } } } /// /// Virtual for unit test purpose. /// protected virtual IEnumerable> GetExtensionNodes(string extensionPath) { return AddinManager.GetExtensionNodes>(extensionPath); } /// /// Virtual for unit test purpose. /// public virtual bool TryGetHandler(string actionId, out THandler handler) { Reference reference; if (m_handlers.TryGetValue(actionId, out reference)) { handler = reference.Value; } else { handler = null; } return handler != null; } /// /// Adds manually the handler in the handler list. /// /// False if an handler was already present for that action id. public bool AddHandler(string actionId, THandler handler) { Reference reference; if (!m_handlers.TryGetValue(actionId, out reference)) { m_handlers.Add(actionId, Reference.FromValue(handler)); return true; } return false; } #endregion } }