feat(ExcelIO): add ExcelMapRegistry for DI integration
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
namespace JdeScoping.ExcelIO.Mapping;
|
||||
|
||||
/// <summary>
|
||||
/// Registry for Excel class maps, used for DI and lookup.
|
||||
/// </summary>
|
||||
public sealed class ExcelMapRegistry
|
||||
{
|
||||
private readonly Dictionary<Type, IExcelClassMap> _maps = new();
|
||||
|
||||
/// <summary>
|
||||
/// Registers a map for a type.
|
||||
/// </summary>
|
||||
public void Register<T>(ExcelClassMap<T> map)
|
||||
{
|
||||
_maps[typeof(T)] = map;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the map for a type.
|
||||
/// </summary>
|
||||
public IExcelClassMap GetMap<T>() => GetMap(typeof(T));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the map for a type.
|
||||
/// </summary>
|
||||
public IExcelClassMap GetMap(Type type)
|
||||
{
|
||||
if (_maps.TryGetValue(type, out var map))
|
||||
return map;
|
||||
|
||||
throw new InvalidOperationException(
|
||||
$"No Excel map registered for type {type.Name}. " +
|
||||
$"Register a map using ExcelMapRegistry.Register().");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a map exists for a type.
|
||||
/// </summary>
|
||||
public bool HasMap<T>() => HasMap(typeof(T));
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a map exists for a type.
|
||||
/// </summary>
|
||||
public bool HasMap(Type type) => _maps.ContainsKey(type);
|
||||
}
|
||||
Reference in New Issue
Block a user