From e58b86c0fb9ffe4534b674629cfe3ea65da57605 Mon Sep 17 00:00:00 2001 From: Mathias Beaulieu-Duncan Date: Mon, 2 Sep 2024 21:28:27 -0400 Subject: [PATCH] fix DynamicQueryController removed Generic that shouldn't have been removed in the first place, causing run time issues with MakeGeneric --- .../Mvc/DynamicQueryController.cs | 4 +- .../DynamicQueryControllerFeatureProvider.cs | 48 +++++++++++++++---- ...Harbor.CQRS.DynamicQuery.AspNetCore.csproj | 2 +- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/OpenHarbor.CQRS.DynamicQuery.AspNetCore/Mvc/DynamicQueryController.cs b/OpenHarbor.CQRS.DynamicQuery.AspNetCore/Mvc/DynamicQueryController.cs index 9d04fb9..aa3afd3 100644 --- a/OpenHarbor.CQRS.DynamicQuery.AspNetCore/Mvc/DynamicQueryController.cs +++ b/OpenHarbor.CQRS.DynamicQuery.AspNetCore/Mvc/DynamicQueryController.cs @@ -8,7 +8,7 @@ using PoweredSoft.DynamicQuery.Core; namespace OpenHarbor.CQRS.DynamicQuery.AspNetCore.Mvc; [ApiController, Route("api/query/[controller]")] -public class DynamicQueryController : Controller +public class DynamicQueryController : Controller where TSource : class where TDestination : class { @@ -34,7 +34,7 @@ public class DynamicQueryController : Controller } [ApiController, Route("api/query/[controller]")] -public class DynamicQueryController : Controller +public class DynamicQueryController : Controller where TSource : class where TDestination : class where TParams : class diff --git a/OpenHarbor.CQRS.DynamicQuery.AspNetCore/Mvc/DynamicQueryControllerFeatureProvider.cs b/OpenHarbor.CQRS.DynamicQuery.AspNetCore/Mvc/DynamicQueryControllerFeatureProvider.cs index 7bb7a01..0241040 100644 --- a/OpenHarbor.CQRS.DynamicQuery.AspNetCore/Mvc/DynamicQueryControllerFeatureProvider.cs +++ b/OpenHarbor.CQRS.DynamicQuery.AspNetCore/Mvc/DynamicQueryControllerFeatureProvider.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Reflection; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Controllers; @@ -9,18 +10,43 @@ using OpenHarbor.CQRS.DynamicQuery.Discover; namespace OpenHarbor.CQRS.DynamicQuery.AspNetCore.Mvc; -public class DynamicQueryControllerFeatureProvider : IApplicationFeatureProvider +public class DynamicQueryControllerFeatureProvider(ServiceProvider serviceProvider) + : IApplicationFeatureProvider { - private readonly ServiceProvider _serviceProvider; + + /** + * public void PopulateFeature(IEnumerable parts, ControllerFeature feature) + { + var queryDiscovery = this.serviceProvider.GetRequiredService(); + foreach (var f in queryDiscovery.GetQueries()) + { + var ignoreAttribute = f.QueryType.GetCustomAttribute(); + if (ignoreAttribute != null) + continue; - public DynamicQueryControllerFeatureProvider(ServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - } + if (f.Category != "DynamicQuery") + continue; + if (f is DynamicQueryMeta dynamicQueryMeta) + { + if (dynamicQueryMeta.ParamsType == null) + { + var controllerType = typeof(DynamicQueryController<,,>).MakeGenericType(f.QueryType, dynamicQueryMeta.SourceType, dynamicQueryMeta.DestinationType); + var controllerTypeInfo = controllerType.GetTypeInfo(); + feature.Controllers.Add(controllerTypeInfo); + } + else + { + var controllerType = typeof(DynamicQueryController<,,,>).MakeGenericType(f.QueryType, dynamicQueryMeta.SourceType, dynamicQueryMeta.DestinationType, dynamicQueryMeta.ParamsType); + var controllerTypeInfo = controllerType.GetTypeInfo(); + feature.Controllers.Add(controllerTypeInfo); + } + } + } + */ public void PopulateFeature(IEnumerable parts, ControllerFeature feature) { - var queryDiscovery = _serviceProvider.GetRequiredService(); + var queryDiscovery = serviceProvider.GetRequiredService(); foreach (var queryMeta in queryDiscovery.GetQueries()) { var ignoreAttribute = queryMeta.QueryType.GetCustomAttribute(); @@ -32,15 +58,19 @@ public class DynamicQueryControllerFeatureProvider : IApplicationFeatureProvider if (queryMeta is DynamicQueryMeta dynamicQueryMeta) { + // todo: add better error output for the user + if (dynamicQueryMeta.ParamsType == null) { - var controllerType = typeof(DynamicQueryController<,>).MakeGenericType(queryMeta.QueryType, dynamicQueryMeta.SourceType, dynamicQueryMeta.DestinationType); + // todo: not aot friendly + var controllerType = typeof(DynamicQueryController<,,>).MakeGenericType(queryMeta.QueryType, dynamicQueryMeta.SourceType, dynamicQueryMeta.DestinationType); var controllerTypeInfo = controllerType.GetTypeInfo(); feature.Controllers.Add(controllerTypeInfo); } else { - var controllerType = typeof(DynamicQueryController<,,>).MakeGenericType(queryMeta.QueryType, dynamicQueryMeta.SourceType, dynamicQueryMeta.DestinationType, dynamicQueryMeta.ParamsType); + // todo: not aot friendly + var controllerType = typeof(DynamicQueryController<,,,>).MakeGenericType(queryMeta.QueryType, dynamicQueryMeta.SourceType, dynamicQueryMeta.DestinationType, dynamicQueryMeta.ParamsType); var controllerTypeInfo = controllerType.GetTypeInfo(); feature.Controllers.Add(controllerTypeInfo); } diff --git a/OpenHarbor.CQRS.DynamicQuery.AspNetCore/OpenHarbor.CQRS.DynamicQuery.AspNetCore.csproj b/OpenHarbor.CQRS.DynamicQuery.AspNetCore/OpenHarbor.CQRS.DynamicQuery.AspNetCore.csproj index 409f6d3..967574d 100644 --- a/OpenHarbor.CQRS.DynamicQuery.AspNetCore/OpenHarbor.CQRS.DynamicQuery.AspNetCore.csproj +++ b/OpenHarbor.CQRS.DynamicQuery.AspNetCore/OpenHarbor.CQRS.DynamicQuery.AspNetCore.csproj @@ -7,7 +7,7 @@ - + gi