using System.Collections.Generic; using System.Reflection; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.Extensions.DependencyInjection; using OpenHarbor.CQRS.Abstractions.Discovery; using OpenHarbor.CQRS.AspNetCore.Abstractions.Attributes; namespace OpenHarbor.CQRS.AspNetCore.Mvc; public class QueryControllerFeatureProvider(ServiceProvider serviceProvider) : IApplicationFeatureProvider { public void PopulateFeature(IEnumerable parts, ControllerFeature feature) { var queryDiscovery = serviceProvider.GetRequiredService(); foreach (var queryMeta in queryDiscovery.GetQueries()) { var ignoreAttribute = queryMeta.QueryType.GetCustomAttribute(); if (ignoreAttribute != null) continue; if (queryMeta.Category != "BasicQuery") continue; var controllerType = typeof(QueryController<,>).MakeGenericType(queryMeta.QueryType, queryMeta.QueryResultType); var controllerTypeInfo = controllerType.GetTypeInfo(); feature.Controllers.Add(controllerTypeInfo); } } }