dotnet-cqrs/Svrnty.CQRS/Configuration/CqrsConfiguration.cs
2025-11-04 16:45:54 -05:00

62 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
namespace Svrnty.CQRS.Configuration;
/// <summary>
/// Configuration for CQRS services and endpoints.
/// Supports extension by third-party packages through generic configuration storage.
/// </summary>
public class CqrsConfiguration
{
private readonly Dictionary<Type, object> _configurations = new();
private readonly List<Action<object>> _mappingCallbacks = new();
/// <summary>
/// Sets a configuration object for a specific type
/// </summary>
public void SetConfiguration<T>(T config) where T : class
{
_configurations[typeof(T)] = config;
}
/// <summary>
/// Gets a configuration object for a specific type
/// </summary>
public T? GetConfiguration<T>() where T : class
{
return _configurations.TryGetValue(typeof(T), out var config) ? config as T : null;
}
/// <summary>
/// Checks if a configuration exists for a specific type
/// </summary>
public bool HasConfiguration<T>() where T : class
{
return _configurations.ContainsKey(typeof(T));
}
/// <summary>
/// Registers a callback to be executed during endpoint mapping.
/// Used by extension packages to register their endpoint mapping logic.
/// </summary>
/// <param name="callback">Callback that accepts an application builder (typically WebApplication)</param>
public void AddMappingCallback(Action<object> callback)
{
_mappingCallbacks.Add(callback);
}
/// <summary>
/// Executes all registered mapping callbacks.
/// Called by UseSvrntyCqrs() to map endpoints from all configured packages.
/// </summary>
/// <param name="app">The application builder (typically WebApplication)</param>
public void ExecuteMappingCallbacks(object app)
{
foreach (var callback in _mappingCallbacks)
{
callback(app);
}
}
}