99 lines
No EOL
2.9 KiB
C#
99 lines
No EOL
2.9 KiB
C#
using System.Collections.Generic;
|
|
using JD.Kernel.Classes;
|
|
using JD.Kernel.StaticHelpers;
|
|
|
|
namespace JD.TeaBoxAddinSystem.Addins
|
|
{
|
|
public class ActionHandlerRepository<THandler>
|
|
where THandler : class
|
|
{
|
|
#region Variables
|
|
|
|
private readonly Dictionary<string, Reference<THandler>> m_handlers = new Dictionary<string, Reference<THandler>>();
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
public ActionHandlerRepository(string extensionPath)
|
|
{
|
|
Throw.IfEmpty(extensionPath, "extensionPath");
|
|
|
|
Init(extensionPath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ctor for unit test purpose.
|
|
/// </summary>
|
|
protected ActionHandlerRepository()
|
|
{
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
protected void Init(string extensionPath)
|
|
{
|
|
System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(extensionPath));
|
|
|
|
foreach (HandlerExtensionNode<THandler> 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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Virtual for unit test purpose.
|
|
/// </summary>
|
|
protected virtual IEnumerable<HandlerExtensionNode<THandler>> GetExtensionNodes(string extensionPath)
|
|
{
|
|
return AddinManager.GetExtensionNodes<HandlerExtensionNode<THandler>>(extensionPath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Virtual for unit test purpose.
|
|
/// </summary>
|
|
public virtual bool TryGetHandler(string actionId, out THandler handler)
|
|
{
|
|
Reference<THandler> reference;
|
|
if (m_handlers.TryGetValue(actionId, out reference))
|
|
{
|
|
handler = reference.Value;
|
|
}
|
|
else
|
|
{
|
|
handler = null;
|
|
}
|
|
|
|
return handler != null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds manually the handler in the handler list.
|
|
/// </summary>
|
|
/// <returns>False if an handler was already present for that action id.</returns>
|
|
public bool AddHandler(string actionId, THandler handler)
|
|
{
|
|
Reference<THandler> reference;
|
|
if (!m_handlers.TryGetValue(actionId, out reference))
|
|
{
|
|
m_handlers.Add(actionId, Reference<THandler>.FromValue(handler));
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |