diff --git a/src/Umbraco.Tests/CacheRefresherFactoryTests.cs b/src/Umbraco.Tests/CacheRefresherFactoryTests.cs new file mode 100644 index 0000000000..aa9685be23 --- /dev/null +++ b/src/Umbraco.Tests/CacheRefresherFactoryTests.cs @@ -0,0 +1,112 @@ +using System; +using System.Linq; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Tests.TestHelpers; +using umbraco.interfaces; + +namespace Umbraco.Tests +{ + [TestFixture] + public class CacheRefresherFactoryTests + { + [SetUp] + public void Initialize() + { + TestHelper.SetupLog4NetForTests(); + + //this ensures its reset + PluginTypeResolver.Current = new PluginTypeResolver(); + + //for testing, we'll specify which assemblies are scanned for the PluginTypeResolver + PluginTypeResolver.Current.AssembliesToScan = new[] + { + this.GetType().Assembly + }; + } + + [Test] + public void Find_All_Refreshers() + { + umbraco.presentation.cache.Factory.Initialize(); + Assert.AreEqual(2, umbraco.presentation.cache.Factory._refreshers.Count); + } + + [Test] + public void Get_All_Instances() + { + umbraco.presentation.cache.Factory.Initialize(); + var factory = new umbraco.presentation.cache.Factory(); + Assert.AreEqual(2, factory.GetAll().Count()); + } + + #region Classes for tests + public class CacheRefresher1 : ICacheRefresher + { + public Guid UniqueIdentifier + { + get { return new Guid("21350A89-4C0D-48B2-B25E-3EE19BFD59FF"); } + } + + public string Name + { + get { return "CacheRefresher1"; } + } + + public void RefreshAll() + { + throw new NotImplementedException(); + } + + public void Refresh(int Id) + { + throw new NotImplementedException(); + } + + public void Remove(int Id) + { + throw new NotImplementedException(); + } + + public void Refresh(Guid Id) + { + throw new NotImplementedException(); + } + } + + public class CacheRefresher2 : ICacheRefresher + { + public Guid UniqueIdentifier + { + get { return new Guid("9266CB73-1FDE-4CD9-8DBC-159C2D39BE5D"); } + } + + public string Name + { + get { return "CacheRefresher2"; } + } + + public void RefreshAll() + { + throw new NotImplementedException(); + } + + public void Refresh(int Id) + { + throw new NotImplementedException(); + } + + public void Remove(int Id) + { + throw new NotImplementedException(); + } + + public void Refresh(Guid Id) + { + 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 03b2504315..ad37c917d3 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -56,6 +56,7 @@ + diff --git a/src/Umbraco.Web/PluginTypeResolverExtensions.cs b/src/Umbraco.Web/PluginTypeResolverExtensions.cs index 4e536dd6e9..58733f3b2d 100644 --- a/src/Umbraco.Web/PluginTypeResolverExtensions.cs +++ b/src/Umbraco.Web/PluginTypeResolverExtensions.cs @@ -44,5 +44,15 @@ namespace Umbraco.Web { return resolver.ResolveAttributedTypes(); } + + /// + /// Returns all classes attributed with XsltExtensionAttribute attribute + /// + /// + /// + internal static IEnumerable ResolveCacheRefreshers(this PluginTypeResolver resolver) + { + return resolver.ResolveTypes(); + } } } \ No newline at end of file diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/cache/factory.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/cache/factory.cs index 65a6807baf..487666972a 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/cache/factory.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/cache/factory.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Concurrent; using System.Web; - +using Umbraco.Core; +using Umbraco.Web; using umbraco.BusinessLogic.Utils; using umbraco.interfaces; using System.Collections.Generic; @@ -14,7 +16,7 @@ namespace umbraco.presentation.cache { #region Declarations - private static readonly Dictionary _refreshers = new Dictionary(); + internal static readonly ConcurrentDictionary _refreshers = new ConcurrentDictionary(); #endregion @@ -32,16 +34,16 @@ namespace umbraco.presentation.cache #region Methods - private static void Initialize() + internal static void Initialize() { - var typeFinder = new Umbraco.Core.TypeFinder2(); - var types = typeFinder.FindClassesOfType(); - foreach (var t in types) - { - var typeInstance = Activator.CreateInstance(t) as ICacheRefresher; - if (typeInstance != null) - _refreshers.Add(typeInstance.UniqueIdentifier, t); - } + foreach(var t in PluginTypeResolver.Current.ResolveCacheRefreshers()) + { + var instance = PluginTypeResolver.Current.CreateInstance(t); + if (instance != null) + { + _refreshers.TryAdd(instance.UniqueIdentifier, t); + } + } } public ICacheRefresher CacheRefresher(Guid CacheRefresherId) @@ -56,26 +58,27 @@ namespace umbraco.presentation.cache /// public ICacheRefresher GetNewObject(Guid CacheRefresherId) { - ICacheRefresher newObject = Activator.CreateInstance(_refreshers[CacheRefresherId]) as ICacheRefresher; - return newObject; + return !_refreshers.ContainsKey(CacheRefresherId) + ? null + : PluginTypeResolver.Current.CreateInstance(_refreshers[CacheRefresherId]); } - /// + /// /// Gets all ICacheRefreshers /// /// public ICacheRefresher[] GetAll() { - ICacheRefresher[] retVal = new ICacheRefresher[_refreshers.Count]; - int c = 0; + var retVal = new ICacheRefresher[_refreshers.Count]; + var c = 0; - foreach (ICacheRefresher cr in _refreshers.Values) - { - retVal[c] = GetNewObject(cr.UniqueIdentifier); - c++; - } + foreach (var id in _refreshers.Keys) + { + retVal[c] = GetNewObject(id); + c++; + } - return retVal; + return retVal; } #endregion