diff --git a/src/Umbraco.Core/Properties/AssemblyInfo.cs b/src/Umbraco.Core/Properties/AssemblyInfo.cs index 222f4e76b8..229c77c8d8 100644 --- a/src/Umbraco.Core/Properties/AssemblyInfo.cs +++ b/src/Umbraco.Core/Properties/AssemblyInfo.cs @@ -22,4 +22,5 @@ using System.Runtime.InteropServices; [assembly: InternalsVisibleTo("Umbraco.Tests")] [assembly: InternalsVisibleTo("businesslogic")] [assembly: InternalsVisibleTo("cms")] +[assembly: InternalsVisibleTo("umbraco.editorControls")] diff --git a/src/Umbraco.Tests/MacroControlFactoryTests.cs b/src/Umbraco.Tests/MacroControlFactoryTests.cs new file mode 100644 index 0000000000..c70e24a599 --- /dev/null +++ b/src/Umbraco.Tests/MacroControlFactoryTests.cs @@ -0,0 +1,61 @@ +using System; +using System.Linq; +using System.Web.UI; +using NUnit.Framework; +using Umbraco.Core; +using umbraco.editorControls.macrocontainer; +using umbraco.interfaces; + +namespace Umbraco.Tests +{ + [TestFixture] + public class MacroControlFactoryTests + { + [SetUp] + public void Initialize() + { + //for testing, we'll specify which assemblies are scanned for the PluginTypeResolver + PluginTypeResolver.Current.AssembliesToScan = new[] + { + this.GetType().Assembly + }; + } + + [Test] + public void Find_Types() + { + var found = MacroControlFactory.MacroControlTypes; + Assert.AreEqual(2, found.Count()); + } + + #region Classes for tests + public class ControlMacroRendering : Control, IMacroGuiRendering + { + public string Value + { + get { throw new NotImplementedException(); } + set { throw new NotImplementedException(); } + } + + public bool ShowCaption + { + get { throw new NotImplementedException(); } + } + } + + public class NonControlMacroRendering : IMacroGuiRendering + { + public string Value + { + get { throw new NotImplementedException(); } + set { throw new NotImplementedException(); } + } + + public bool ShowCaption + { + get { throw new NotImplementedException(); } + } + } + #endregion + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 89195b7179..6e02fd0446 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -50,6 +50,7 @@ + diff --git a/src/umbraco.editorControls/PluginTypeResolverExtensions.cs b/src/umbraco.editorControls/PluginTypeResolverExtensions.cs new file mode 100644 index 0000000000..ae557087a1 --- /dev/null +++ b/src/umbraco.editorControls/PluginTypeResolverExtensions.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using Umbraco.Core; +using umbraco.BusinessLogic.Actions; +using umbraco.cms.businesslogic.macro; +using umbraco.cms.businesslogic.media; +using umbraco.interfaces; + +namespace umbraco.editorControls +{ + /// + /// Extension methods for the PluginTypeResolver + /// + public static class PluginTypeResolverExtensions + { + + /// + /// Returns all available IMacroGuiRendering in application + /// + /// + /// + internal static IEnumerable ResolveMacroRenderings(this PluginTypeResolver resolver) + { + return resolver.ResolveTypes(); + } + + + } +} \ No newline at end of file diff --git a/src/umbraco.editorControls/Properties/AssemblyInfo.cs b/src/umbraco.editorControls/Properties/AssemblyInfo.cs index 15de1fa37b..e84fc669e0 100644 --- a/src/umbraco.editorControls/Properties/AssemblyInfo.cs +++ b/src/umbraco.editorControls/Properties/AssemblyInfo.cs @@ -10,3 +10,5 @@ using System.Runtime.CompilerServices; [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyProduct("")] + +[assembly: InternalsVisibleTo("Umbraco.Tests")] \ No newline at end of file diff --git a/src/umbraco.editorControls/macrocontainer/MacroControlFactory.cs b/src/umbraco.editorControls/macrocontainer/MacroControlFactory.cs index e7b7ff1f6a..da2b1d301b 100644 --- a/src/umbraco.editorControls/macrocontainer/MacroControlFactory.cs +++ b/src/umbraco.editorControls/macrocontainer/MacroControlFactory.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Web.UI; +using Umbraco.Core; using umbraco.BusinessLogic.Utils; using umbraco.interfaces; using umbraco.editorControls; @@ -12,14 +14,17 @@ namespace umbraco.editorControls.macrocontainer { internal class MacroControlFactory { - #region Private Fields + /// /// All Possible Macro types /// private static List _macroControlTypes = null; - #endregion + + private static readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim(); + #region Methods + /// /// Create an instance of a Macro control and return it. /// Because the macro control uses inline client script whichs is not generated after postback @@ -27,19 +32,22 @@ namespace umbraco.editorControls.macrocontainer /// internal static Control GetMacroRenderControlByType(PersistableMacroProperty prop, string uniqueID) { - Control macroControl; - - Type m = MacroControlTypes.FindLast(delegate(Type macroGuiCcontrol) { return macroGuiCcontrol.ToString() == string.Format("{0}.{1}", prop.AssemblyName, prop.TypeName); }); - IMacroGuiRendering typeInstance; - typeInstance = Activator.CreateInstance(m) as IMacroGuiRendering; - if (!string.IsNullOrEmpty(prop.Value)) - { - ((IMacroGuiRendering)typeInstance).Value = prop.Value; - } - macroControl = (Control)typeInstance; - - macroControl.ID = uniqueID; - return macroControl; + var m = MacroControlTypes.FindLast(macroGuiCcontrol => macroGuiCcontrol.ToString() == string.Format("{0}.{1}", prop.AssemblyName, prop.TypeName)); + var instance = PluginTypeResolver.Current.CreateInstance(m); + if (instance != null) + { + if (!string.IsNullOrEmpty(prop.Value)) + { + instance.Value = prop.Value; + } + var macroControl = instance as Control; + if (macroControl != null) + { + macroControl.ID = uniqueID; + return macroControl; + } + } + return null; } /// @@ -51,25 +59,29 @@ namespace umbraco.editorControls.macrocontainer { return ((IMacroGuiRendering)macroControl).Value; } + #endregion #region Properties /// /// All Possible Macro types /// - private static List MacroControlTypes + internal static List MacroControlTypes { get { - if (_macroControlTypes == null || !_macroControlTypes.Any()) - { - //Populate the list with all the types of IMacroGuiRendering - var typeFinder = new Umbraco.Core.TypeFinder2(); - _macroControlTypes = new List(); - _macroControlTypes = typeFinder.FindClassesOfType().ToList(); - } + using (var readLock = new UpgradeableReadLock(Lock)) + { + if (_macroControlTypes == null || !_macroControlTypes.Any()) + { - return _macroControlTypes; + readLock.UpgradeToWriteLock(); + + _macroControlTypes = new List(PluginTypeResolver.Current.ResolveMacroRenderings()); + } + + return _macroControlTypes; + } } } #endregion diff --git a/src/umbraco.editorControls/umbraco.editorControls.csproj b/src/umbraco.editorControls/umbraco.editorControls.csproj index cfdcd26b37..e3a392ee94 100644 --- a/src/umbraco.editorControls/umbraco.editorControls.csproj +++ b/src/umbraco.editorControls/umbraco.editorControls.csproj @@ -176,6 +176,7 @@ + Code