From 1823cce4f69a774d112ac0c9ab17c2207605da0d Mon Sep 17 00:00:00 2001 From: "shannon@ShandemVaio" Date: Fri, 27 Jul 2012 10:53:40 +0600 Subject: [PATCH] Changed over Action to use PluginTypeResolver to find and instantiate IActionHandlers and included a unit test. --- src/Umbraco.Core/PluginTypeResolver.cs | 27 ++++++++++++++++--- src/Umbraco.Core/Properties/AssemblyInfo.cs | 1 + src/Umbraco.Tests/PluginTypeResolverTests.cs | 10 +++++++ .../Routing/DocumentLookupsResolver.cs | 2 +- src/umbraco.cms/Actions/Action.cs | 16 +++-------- .../PluginTypeResolverExtensions.cs | 27 +++++++++++++++++++ src/umbraco.cms/Properties/AssemblyInfo.cs | 3 ++- src/umbraco.cms/umbraco.cms.csproj | 1 + 8 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 src/umbraco.cms/PluginTypeResolverExtensions.cs diff --git a/src/Umbraco.Core/PluginTypeResolver.cs b/src/Umbraco.Core/PluginTypeResolver.cs index be16fc6ff3..50df96c5c2 100644 --- a/src/Umbraco.Core/PluginTypeResolver.cs +++ b/src/Umbraco.Core/PluginTypeResolver.cs @@ -10,7 +10,7 @@ namespace Umbraco.Core { /// - /// Used to resolve all plugin types + /// Used to resolve all plugin types and cache them /// /// /// @@ -62,16 +62,29 @@ namespace Umbraco.Core set { _assemblies = value; } } + /// + /// Used to resolve and create instances of the specified type based on the resolved/cached plugin types + /// + /// + /// set to true if an exception is to be thrown if there is an error during instantiation + /// + internal IEnumerable FindAndCreateInstances(bool throwException = false) + { + var types = ResolveTypes(); + return CreateInstances(types, throwException); + } + /// /// Used to create instances of the specified type based on the resolved/cached plugin types /// /// + /// + /// set to true if an exception is to be thrown if there is an error during instantiation /// - internal IEnumerable CreateInstances() + internal IEnumerable CreateInstances(IEnumerable types, bool throwException = false) { - var types = ResolveTypes(); var instances = new List(); - foreach(var t in types) + foreach (var t in types) { try { @@ -82,6 +95,10 @@ namespace Umbraco.Core { //TODO: Need to fix logging so this doesn't bork if no SQL connection //Log.Add(LogTypes.Error, -1, "Error loading ILookup: " + ex.ToString()); + if (throwException) + { + throw ex; + } } } return instances; @@ -143,6 +160,8 @@ namespace Umbraco.Core return _types; } + + #region Private classes internal abstract class TypeList { diff --git a/src/Umbraco.Core/Properties/AssemblyInfo.cs b/src/Umbraco.Core/Properties/AssemblyInfo.cs index 49c9f88d05..222f4e76b8 100644 --- a/src/Umbraco.Core/Properties/AssemblyInfo.cs +++ b/src/Umbraco.Core/Properties/AssemblyInfo.cs @@ -21,4 +21,5 @@ using System.Runtime.InteropServices; [assembly: InternalsVisibleTo("umbraco")] [assembly: InternalsVisibleTo("Umbraco.Tests")] [assembly: InternalsVisibleTo("businesslogic")] +[assembly: InternalsVisibleTo("cms")] diff --git a/src/Umbraco.Tests/PluginTypeResolverTests.cs b/src/Umbraco.Tests/PluginTypeResolverTests.cs index b5428e42e7..96e0ede87a 100644 --- a/src/Umbraco.Tests/PluginTypeResolverTests.cs +++ b/src/Umbraco.Tests/PluginTypeResolverTests.cs @@ -9,9 +9,12 @@ using umbraco.MacroEngines.Iron; using umbraco.businesslogic; using umbraco.cms.businesslogic; using umbraco.uicontrols; +using umbraco.cms; namespace Umbraco.Tests { + + [TestFixture] public class PluginTypeResolverTests { @@ -71,6 +74,13 @@ namespace Umbraco.Tests Assert.AreEqual(7, apps.Count()); } + [Test] + public void Resolves_Actions() + { + var types = PluginTypeResolver.Current.ResolveActions(); + Assert.AreEqual(1, types.Count()); + } + public interface IFindMe { diff --git a/src/Umbraco.Web/Routing/DocumentLookupsResolver.cs b/src/Umbraco.Web/Routing/DocumentLookupsResolver.cs index 0e6cae28d2..642c79f9fc 100644 --- a/src/Umbraco.Web/Routing/DocumentLookupsResolver.cs +++ b/src/Umbraco.Web/Routing/DocumentLookupsResolver.cs @@ -14,7 +14,7 @@ namespace Umbraco.Web.Routing internal DocumentLookupsResolver(IEnumerable resolvers, IRequestDocumentLastChanceResolver lastChanceResolver) { //TODO: I've changed this to resolve types but the intances are not created yet! - // I've created a method on the PluginTypeResolver to create types: PluginTypesResolver.Current.CreateInstances() + // I've created a method on the PluginTypeResolver to create types: PluginTypesResolver.Current.FindAndCreateInstances() _resolverTypes.AddRange(resolvers); diff --git a/src/umbraco.cms/Actions/Action.cs b/src/umbraco.cms/Actions/Action.cs index b7cae428d8..0622709b3b 100644 --- a/src/umbraco.cms/Actions/Action.cs +++ b/src/umbraco.cms/Actions/Action.cs @@ -6,6 +6,7 @@ using System.Reflection; using Umbraco.Core; using umbraco.BasePages; using umbraco.BusinessLogic.Utils; +using umbraco.cms; using umbraco.cms.businesslogic.web; using umbraco.cms.businesslogic.workflow; using umbraco.interfaces; @@ -35,9 +36,6 @@ namespace umbraco.BusinessLogic.Actions private static readonly Dictionary ActionJs = new Dictionary(); private static readonly object Lock = new object(); - private static readonly object LockerReRegister = new object(); - - private static readonly Umbraco.Core.TypeFinder2 TypeFinder = new TypeFinder2(); static Action() { @@ -64,17 +62,11 @@ namespace umbraco.BusinessLogic.Actions /// private static void RegisterIActionHandlers() { - if (ActionHandlers.Count == 0) { - - var foundIActionHandlers = TypeFinder.FindClassesOfType(); - foreach (var type in foundIActionHandlers) - { - var typeInstance = Activator.CreateInstance(type) as IActionHandler; - if (typeInstance != null) - ActionHandlers.Add(typeInstance); - } + ActionHandlers.AddRange( + PluginTypeResolver.Current.CreateInstances( + PluginTypeResolver.Current.ResolveActions())); } } diff --git a/src/umbraco.cms/PluginTypeResolverExtensions.cs b/src/umbraco.cms/PluginTypeResolverExtensions.cs new file mode 100644 index 0000000000..1ccda0c54d --- /dev/null +++ b/src/umbraco.cms/PluginTypeResolverExtensions.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using Umbraco.Core; +using umbraco.BusinessLogic.Actions; +using umbraco.businesslogic; +using umbraco.interfaces; + +namespace umbraco.cms +{ + /// + /// Extension methods for the PluginTypeResolver + /// + public static class PluginTypeResolverExtensions + { + + /// + /// Returns all available IActionHandler in application + /// + /// + /// + internal static IEnumerable ResolveActions(this PluginTypeResolver resolver) + { + return resolver.ResolveTypes(); + } + + } +} \ No newline at end of file diff --git a/src/umbraco.cms/Properties/AssemblyInfo.cs b/src/umbraco.cms/Properties/AssemblyInfo.cs index c38a6bd99c..9cbdba2843 100644 --- a/src/umbraco.cms/Properties/AssemblyInfo.cs +++ b/src/umbraco.cms/Properties/AssemblyInfo.cs @@ -13,4 +13,5 @@ using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("umbraco", AllInternalsVisible=true)] -[assembly: InternalsVisibleTo("Umbraco.LegacyTests")] \ No newline at end of file +[assembly: InternalsVisibleTo("Umbraco.LegacyTests")] +[assembly: InternalsVisibleTo("Umbraco.Tests")] \ No newline at end of file diff --git a/src/umbraco.cms/umbraco.cms.csproj b/src/umbraco.cms/umbraco.cms.csproj index 068836f738..bdd3b9639d 100644 --- a/src/umbraco.cms/umbraco.cms.csproj +++ b/src/umbraco.cms/umbraco.cms.csproj @@ -267,6 +267,7 @@ + Code