fix DynamicQueryController removed Generic that shouldn't have been removed in the first place, causing run time issues with MakeGeneric

This commit is contained in:
Mathias Beaulieu-Duncan 2024-09-02 21:28:27 -04:00
parent c230d039f2
commit e58b86c0fb
Signed by: mathias
GPG Key ID: 1C16CF05BAF9162D
3 changed files with 42 additions and 12 deletions

View File

@ -8,7 +8,7 @@ using PoweredSoft.DynamicQuery.Core;
namespace OpenHarbor.CQRS.DynamicQuery.AspNetCore.Mvc;
[ApiController, Route("api/query/[controller]")]
public class DynamicQueryController<TSource, TDestination> : Controller
public class DynamicQueryController<TUnderlyingQuery, TSource, TDestination> : Controller
where TSource : class
where TDestination : class
{
@ -34,7 +34,7 @@ public class DynamicQueryController<TSource, TDestination> : Controller
}
[ApiController, Route("api/query/[controller]")]
public class DynamicQueryController<TSource, TDestination, TParams> : Controller
public class DynamicQueryController<TUnderlyingQuery, TSource, TDestination, TParams> : Controller
where TSource : class
where TDestination : class
where TParams : class

View File

@ -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<ControllerFeature>
public class DynamicQueryControllerFeatureProvider(ServiceProvider serviceProvider)
: IApplicationFeatureProvider<ControllerFeature>
{
private readonly ServiceProvider _serviceProvider;
public DynamicQueryControllerFeatureProvider(ServiceProvider serviceProvider)
/**
* public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
{
_serviceProvider = serviceProvider;
var queryDiscovery = this.serviceProvider.GetRequiredService<IQueryDiscovery>();
foreach (var f in queryDiscovery.GetQueries())
{
var ignoreAttribute = f.QueryType.GetCustomAttribute<QueryControllerIgnoreAttribute>();
if (ignoreAttribute != null)
continue;
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<ApplicationPart> parts, ControllerFeature feature)
{
var queryDiscovery = _serviceProvider.GetRequiredService<IQueryDiscovery>();
var queryDiscovery = serviceProvider.GetRequiredService<IQueryDiscovery>();
foreach (var queryMeta in queryDiscovery.GetQueries())
{
var ignoreAttribute = queryMeta.QueryType.GetCustomAttribute<QueryControllerIgnoreAttribute>();
@ -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);
}

View File

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />gi
</ItemGroup>
<ItemGroup>