From 9f7fae2ca6265e5e84c7155748591706345de4b3 Mon Sep 17 00:00:00 2001 From: Stephan Date: Tue, 5 Mar 2019 19:23:57 +0100 Subject: [PATCH] Enable adding api controllers --- .../Composing/TypeCollectionBuilderBase.cs | 44 +++++++++++++++++++ src/Umbraco.Core/Umbraco.Core.csproj | 1 + .../Mvc/SurfaceControllerTypeCollection.cs | 4 -- .../SurfaceControllerTypeCollectionBuilder.cs | 8 ++++ src/Umbraco.Web/Runtime/WebRuntimeComposer.cs | 11 +++-- src/Umbraco.Web/Umbraco.Web.csproj | 2 + .../UmbracoApiControllerTypeCollection.cs | 4 -- ...bracoApiControllerTypeCollectionBuilder.cs | 7 +++ 8 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 src/Umbraco.Core/Composing/TypeCollectionBuilderBase.cs create mode 100644 src/Umbraco.Web/Mvc/SurfaceControllerTypeCollectionBuilder.cs create mode 100644 src/Umbraco.Web/WebApi/UmbracoApiControllerTypeCollectionBuilder.cs diff --git a/src/Umbraco.Core/Composing/TypeCollectionBuilderBase.cs b/src/Umbraco.Core/Composing/TypeCollectionBuilderBase.cs new file mode 100644 index 0000000000..35f85c9c2f --- /dev/null +++ b/src/Umbraco.Core/Composing/TypeCollectionBuilderBase.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; + +namespace Umbraco.Core.Composing +{ + /// + /// Provides a base class for collections of types. + /// + public abstract class TypeCollectionBuilderBase : ICollectionBuilder + where TCollection : class, IBuilderCollection + { + private readonly HashSet _types = new HashSet(); + + 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() => Add(typeof(T)); + + public void Add(IEnumerable types) + { + foreach (var type in types) Add(type); + } + + public void Remove(Type type) => _types.Remove(Validate(type, "remove")); + + public void Remove() => Remove(typeof(T)); + + public TCollection CreateCollection(IFactory factory) + { + return factory.CreateInstance(_types); + } + + public void RegisterWith(IRegister register) + { + register.Register(CreateCollection, Lifetime.Singleton); + } + } +} diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index c0149ccadb..9566293ac1 100755 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -214,6 +214,7 @@ + diff --git a/src/Umbraco.Web/Mvc/SurfaceControllerTypeCollection.cs b/src/Umbraco.Web/Mvc/SurfaceControllerTypeCollection.cs index d9577417cc..59b8adc950 100644 --- a/src/Umbraco.Web/Mvc/SurfaceControllerTypeCollection.cs +++ b/src/Umbraco.Web/Mvc/SurfaceControllerTypeCollection.cs @@ -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 { public SurfaceControllerTypeCollection(IEnumerable items) diff --git a/src/Umbraco.Web/Mvc/SurfaceControllerTypeCollectionBuilder.cs b/src/Umbraco.Web/Mvc/SurfaceControllerTypeCollectionBuilder.cs new file mode 100644 index 0000000000..7ea7472a87 --- /dev/null +++ b/src/Umbraco.Web/Mvc/SurfaceControllerTypeCollectionBuilder.cs @@ -0,0 +1,8 @@ +using Umbraco.Core.Composing; +using Umbraco.Web.WebApi; + +namespace Umbraco.Web.Mvc +{ + public class SurfaceControllerTypeCollectionBuilder : TypeCollectionBuilderBase + { } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Runtime/WebRuntimeComposer.cs b/src/Umbraco.Web/Runtime/WebRuntimeComposer.cs index c8fc209768..2085a1cc55 100644 --- a/src/Umbraco.Web/Runtime/WebRuntimeComposer.cs +++ b/src/Umbraco.Web/Runtime/WebRuntimeComposer.cs @@ -156,12 +156,11 @@ namespace Umbraco.Web.Runtime .Add(() => composition.TypeLoader.GetTypes()); //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() + .Add(composition.TypeLoader.GetSurfaceControllers()); + var umbracoApiControllerTypes = composition.TypeLoader.GetUmbracoApiControllers().ToList(); + composition.WithCollectionBuilder() + .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 diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index b51d688e73..2c164b5b3d 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -204,6 +204,7 @@ + @@ -603,6 +604,7 @@ + diff --git a/src/Umbraco.Web/WebApi/UmbracoApiControllerTypeCollection.cs b/src/Umbraco.Web/WebApi/UmbracoApiControllerTypeCollection.cs index cdb1aa2f51..dc8d1767dc 100644 --- a/src/Umbraco.Web/WebApi/UmbracoApiControllerTypeCollection.cs +++ b/src/Umbraco.Web/WebApi/UmbracoApiControllerTypeCollection.cs @@ -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 { public UmbracoApiControllerTypeCollection(IEnumerable items) diff --git a/src/Umbraco.Web/WebApi/UmbracoApiControllerTypeCollectionBuilder.cs b/src/Umbraco.Web/WebApi/UmbracoApiControllerTypeCollectionBuilder.cs new file mode 100644 index 0000000000..190fcdb00e --- /dev/null +++ b/src/Umbraco.Web/WebApi/UmbracoApiControllerTypeCollectionBuilder.cs @@ -0,0 +1,7 @@ +using Umbraco.Core.Composing; + +namespace Umbraco.Web.WebApi +{ + public class UmbracoApiControllerTypeCollectionBuilder : TypeCollectionBuilderBase + { } +} \ No newline at end of file