93 lines
3.7 KiB
C#
93 lines
3.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Svrnty.CQRS.Configuration;
|
|
|
|
namespace Svrnty.CQRS.MinimalApi;
|
|
|
|
/// <summary>
|
|
/// Extension methods for CqrsBuilder to add MinimalApi support
|
|
/// </summary>
|
|
public static class CqrsBuilderExtensions
|
|
{
|
|
/// <summary>
|
|
/// Adds MinimalApi support to the CQRS pipeline
|
|
/// </summary>
|
|
/// <param name="builder">The CQRS builder</param>
|
|
/// <param name="configure">Optional configuration for MinimalApi endpoints</param>
|
|
/// <returns>The CQRS builder for method chaining</returns>
|
|
public static CqrsBuilder AddMinimalApi(this CqrsBuilder builder, Action<MinimalApiCqrsOptions>? configure = null)
|
|
{
|
|
var options = new MinimalApiCqrsOptions();
|
|
configure?.Invoke(options);
|
|
builder.Configuration.SetConfiguration(options);
|
|
|
|
// Register mapping callback for automatic endpoint mapping
|
|
builder.Configuration.AddMappingCallback(app =>
|
|
{
|
|
if (app is not WebApplication webApp)
|
|
return;
|
|
|
|
var config = webApp.Services.GetService(typeof(CqrsConfiguration)) as CqrsConfiguration;
|
|
var minimalApiOptions = config?.GetConfiguration<MinimalApiCqrsOptions>();
|
|
|
|
if (minimalApiOptions == null)
|
|
return;
|
|
|
|
// Map commands if enabled
|
|
if (minimalApiOptions.MapCommands)
|
|
{
|
|
webApp.MapSvrntyCommands(minimalApiOptions.CommandRoutePrefix);
|
|
}
|
|
|
|
// Map queries if enabled
|
|
if (minimalApiOptions.MapQueries)
|
|
{
|
|
webApp.MapSvrntyQueries(minimalApiOptions.QueryRoutePrefix);
|
|
}
|
|
|
|
// Map dynamic queries if enabled (automatically included)
|
|
if (minimalApiOptions.MapDynamicQueries)
|
|
{
|
|
// Try to find the DynamicQuery.MinimalApi assembly and call MapSvrntyDynamicQueries
|
|
try
|
|
{
|
|
// Force load the assembly by looking for a known type from it
|
|
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
|
var dynamicQueryAssembly = assemblies.FirstOrDefault(a => a.GetName().Name == "Svrnty.CQRS.DynamicQuery.MinimalApi");
|
|
|
|
if (dynamicQueryAssembly == null)
|
|
{
|
|
// Try to load it by finding the type
|
|
var extensionType = Type.GetType("Svrnty.CQRS.DynamicQuery.MinimalApi.EndpointRouteBuilderExtensions, Svrnty.CQRS.DynamicQuery.MinimalApi");
|
|
if (extensionType != null)
|
|
{
|
|
dynamicQueryAssembly = extensionType.Assembly;
|
|
}
|
|
}
|
|
|
|
if (dynamicQueryAssembly != null)
|
|
{
|
|
var extensionType = dynamicQueryAssembly.GetType("Svrnty.CQRS.DynamicQuery.MinimalApi.EndpointRouteBuilderExtensions");
|
|
if (extensionType != null)
|
|
{
|
|
var mapMethod = extensionType.GetMethod("MapSvrntyDynamicQueries", BindingFlags.Public | BindingFlags.Static);
|
|
if (mapMethod != null)
|
|
{
|
|
mapMethod.Invoke(null, new object[] { webApp, minimalApiOptions.DynamicQueryRoutePrefix });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Warning: Could not map dynamic queries: {ex.Message}");
|
|
}
|
|
}
|
|
});
|
|
|
|
return builder;
|
|
}
|
|
}
|