Enable adding api controllers

This commit is contained in:
Stephan
2019-03-05 19:23:57 +01:00
parent 13b35ec7ce
commit 9f7fae2ca6
8 changed files with 67 additions and 14 deletions

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
namespace Umbraco.Core.Composing
{
/// <summary>
/// Provides a base class for collections of types.
/// </summary>
public abstract class TypeCollectionBuilderBase<TCollection, TConstraint> : ICollectionBuilder<TCollection, Type>
where TCollection : class, IBuilderCollection<Type>
{
private readonly HashSet<Type> _types = new HashSet<Type>();
private Type Validate(Type type, string action)
{
if (!typeof(TConstraint).IsAssignableFrom(type))
throw new InvalidOperationException($"Cannot {action} type {type.FullName} as it does not inherit from/implement {typeof(TConstraint).FullName}.");
return type;
}
public void Add(Type type) => _types.Add(Validate(type, "add"));
public void Add<T>() => Add(typeof(T));
public void Add(IEnumerable<Type> types)
{
foreach (var type in types) Add(type);
}
public void Remove(Type type) => _types.Remove(Validate(type, "remove"));
public void Remove<T>() => Remove(typeof(T));
public TCollection CreateCollection(IFactory factory)
{
return factory.CreateInstance<TCollection>(_types);
}
public void RegisterWith(IRegister register)
{
register.Register(CreateCollection, Lifetime.Singleton);
}
}
}

View File

@@ -214,6 +214,7 @@
<Compile Include="Migrations\Upgrade\V_8_0_0\RenameLabelAndRichTextPropertyEditorAliases.cs" />
<Compile Include="PublishedModelFactoryExtensions.cs" />
<Compile Include="Services\PropertyValidationService.cs" />
<Compile Include="Composing\TypeCollectionBuilderBase.cs" />
<Compile Include="TypeLoaderExtensions.cs" />
<Compile Include="Composing\WeightAttribute.cs" />
<Compile Include="Composing\WeightedCollectionBuilderBase.cs" />

View File

@@ -4,10 +4,6 @@ using Umbraco.Core.Composing;
namespace Umbraco.Web.Mvc
{
// unless we want to modify the content of the collection
// which we are not doing at the moment
// we can inherit from BuilderCollectionBase and just be enumerable
public class SurfaceControllerTypeCollection : BuilderCollectionBase<Type>
{
public SurfaceControllerTypeCollection(IEnumerable<Type> items)

View File

@@ -0,0 +1,8 @@
using Umbraco.Core.Composing;
using Umbraco.Web.WebApi;
namespace Umbraco.Web.Mvc
{
public class SurfaceControllerTypeCollectionBuilder : TypeCollectionBuilderBase<SurfaceControllerTypeCollection, SurfaceController>
{ }
}

View File

@@ -156,12 +156,11 @@ namespace Umbraco.Web.Runtime
.Add(() => composition.TypeLoader.GetTypes<IAction>());
//we need to eagerly scan controller types since they will need to be routed
var surfaceControllerTypes = new SurfaceControllerTypeCollection(composition.TypeLoader.GetSurfaceControllers());
composition.RegisterUnique(surfaceControllerTypes);
//we need to eagerly scan controller types since they will need to be routed
var umbracoApiControllerTypes = new UmbracoApiControllerTypeCollection(composition.TypeLoader.GetUmbracoApiControllers());
composition.RegisterUnique(umbracoApiControllerTypes);
composition.WithCollectionBuilder<SurfaceControllerTypeCollectionBuilder>()
.Add(composition.TypeLoader.GetSurfaceControllers());
var umbracoApiControllerTypes = composition.TypeLoader.GetUmbracoApiControllers().ToList();
composition.WithCollectionBuilder<UmbracoApiControllerTypeCollectionBuilder>()
.Add(umbracoApiControllerTypes);
// both TinyMceValueConverter (in Core) and RteMacroRenderingValueConverter (in Web) will be
// discovered when CoreBootManager configures the converters. We HAVE to remove one of them

View File

@@ -204,6 +204,7 @@
<Compile Include="Models\ContentEditing\MacroParameterDisplay.cs" />
<Compile Include="Models\PublishedContent\HybridVariationContextAccessor.cs" />
<Compile Include="Models\TemplateQuery\QueryConditionExtensions.cs" />
<Compile Include="Mvc\SurfaceControllerTypeCollectionBuilder.cs" />
<Compile Include="Routing\IPublishedRouter.cs" />
<Compile Include="Services\DashboardService.cs" />
<Compile Include="Services\IDashboardService.cs" />
@@ -603,6 +604,7 @@
<Compile Include="Editors\Filters\MemberSaveValidationAttribute.cs" />
<Compile Include="WebApi\SessionHttpControllerRouteHandler.cs" />
<Compile Include="WebApi\UmbracoApiControllerTypeCollection.cs" />
<Compile Include="WebApi\UmbracoApiControllerTypeCollectionBuilder.cs" />
<Compile Include="WebApi\UnhandedExceptionLoggerConfigurationAttribute.cs" />
<Compile Include="WebApi\UnhandledExceptionLogger.cs" />
<Compile Include="Runtime\WebRuntimeComponent.cs" />

View File

@@ -4,10 +4,6 @@ using Umbraco.Core.Composing;
namespace Umbraco.Web.WebApi
{
// unless we want to modify the content of the collection
// which we are not doing at the moment
// we can inherit from BuilderCollectionBase and just be enumerable
public class UmbracoApiControllerTypeCollection : BuilderCollectionBase<Type>
{
public UmbracoApiControllerTypeCollection(IEnumerable<Type> items)

View File

@@ -0,0 +1,7 @@
using Umbraco.Core.Composing;
namespace Umbraco.Web.WebApi
{
public class UmbracoApiControllerTypeCollectionBuilder : TypeCollectionBuilderBase<UmbracoApiControllerTypeCollection, UmbracoApiController>
{ }
}