using System;
using System.Collections.Generic;
namespace FrameworkBLL.ActionProcessor.Handlers
{
///
/// Resolves IActionHandler implementations by ActionType.
/// Handlers are registered via dependency injection and mapped
/// internally for fast lookup.
///
public sealed class ActionHandlerResolver : IActionHandlerResolver
{
private readonly IReadOnlyDictionary _map;
public ActionHandlerResolver(IEnumerable handlers)
{
if (handlers == null)
throw new ArgumentNullException(nameof(handlers));
var map = new Dictionary();
foreach (var handler in handlers)
{
if (handler == null)
continue;
if (map.ContainsKey(handler.ActionType))
{
throw new InvalidOperationException(
$"Duplicate IActionHandler registration for ActionType '{handler.ActionType}'.");
}
map.Add(handler.ActionType, handler);
}
_map = map;
}
///
/// Returns the handler for the specified action type.
/// Returns null if no handler is registered.
///
public IActionHandler? Resolve(int actionType)
{
return _map.TryGetValue(actionType, out var handler)
? handler
: null;
}
}
}