load generics by scanning registered services first and fallback to assembly discovery

This commit is contained in:
2020-08-21 11:29:54 +02:00
parent 27a9d6b1c8
commit 4009f2b68a
5 changed files with 41 additions and 8 deletions
+1
View File
@@ -26,6 +26,7 @@ namespace zero.Core.Api
/// <inheritdoc />
public async Task<T> GetById(string id)
{
var res = await GetById<IPage>(id); // this works
return await GetById<T>(id);
}
+13
View File
@@ -0,0 +1,13 @@
export default {
alias: 'page.redirect',
fields: [
{
field: 'link',
display: 'text',
label: 'Link',
required: true
},
]
};
@@ -3,7 +3,7 @@
<img v-if="imageSource" :src="imageSource" class="-image" />
<figcaption>
<article class="-text" v-if="text" v-html="text"></article>
<article class="-subline" v-if="subline" v-html="subline"></article>
<article class="-subline" v-if="subline">{{subline.replace(/<\/?[^>]+>/ig, " ")}}</article>
</figcaption>
</figure>
</template>
+25 -6
View File
@@ -1,11 +1,13 @@
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using zero.Core;
using zero.Core.Assemblies;
using zero.Core.Entities;
using zero.Web.Controllers;
using zero.Web.Filters;
@@ -13,14 +15,18 @@ namespace zero.Web
{
public class ApiControllerFeatureProvider : IApplicationFeatureProvider<ControllerFeature>
{
public ApiControllerFeatureProvider()
{
IServiceCollection ServiceCollection;
public ApiControllerFeatureProvider(IServiceCollection serviceCollection)
{
ServiceCollection = serviceCollection;
}
public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
{
{
var pagetype = typeof(IPage);
var page = ServiceCollection.FirstOrDefault(x => x.ServiceType.Equals(pagetype));
IEnumerable<TypeInfo> candidates = AssemblyDiscovery.Current.GetTypes<ISupportsGenericsController>(parts, true).Where(x => x.ContainsGenericParameters);
foreach (TypeInfo candidate in candidates)
@@ -42,12 +48,25 @@ namespace zero.Web
break;
}
TypeInfo concreteArgument = AssemblyDiscovery.Current.GetTypes(requiredService, parts).FirstOrDefault();
TypeInfo concreteArgument = null;
ServiceDescriptor descriptor = ServiceCollection.FirstOrDefault(x => x.ServiceType.Equals(requiredService));
if (descriptor != null)
{
concreteArgument = descriptor.ImplementationType?.GetTypeInfo();
}
if (concreteArgument == null)
{
isValid = false;
break;
IEnumerable<TypeInfo> concreteArgumentCandidates = AssemblyDiscovery.Current.GetTypes(requiredService, parts);
concreteArgument = concreteArgumentCandidates.FirstOrDefault();
if (concreteArgument == null)
{
isValid = false;
break;
}
}
concreteArguments.Add(concreteArgument);
+1 -1
View File
@@ -79,7 +79,7 @@ namespace zero.Web
{
Mvc.AddRazorRuntimeCompilation();
}
Mvc.PartManager.FeatureProviders.Add(new ApiControllerFeatureProvider());
Mvc.PartManager.FeatureProviders.Add(new ApiControllerFeatureProvider(Services));
// configure Raven + Identity