From feb9f40237052c9fd87be6cbbd48cd54d57878f4 Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 3 Mar 2014 04:16:09 +0100 Subject: [PATCH] updates some ctors on umbraco context and routing context to have lazy overloads which allows us to more easily create/mock the umbraco context. --- src/Umbraco.Tests/MockTests.cs | 1 - src/Umbraco.Web/Routing/RoutingContext.cs | 56 +++++++++++++++------ src/Umbraco.Web/UmbracoContext.cs | 60 ++++++++++++++++------- 3 files changed, 83 insertions(+), 34 deletions(-) diff --git a/src/Umbraco.Tests/MockTests.cs b/src/Umbraco.Tests/MockTests.cs index a3e387ad1e..943b0004f7 100644 --- a/src/Umbraco.Tests/MockTests.cs +++ b/src/Umbraco.Tests/MockTests.cs @@ -109,7 +109,6 @@ namespace Umbraco.Tests Assert.AreNotEqual(appCtx, result); } - [NUnit.Framework.Ignore("Need to fix more stuff up, this is ignore because an exception occurs because it wants to ensure we have a resolver initialized - need to make that process better for testability")] [Test] public void Can_Get_Umbraco_Context() { diff --git a/src/Umbraco.Web/Routing/RoutingContext.cs b/src/Umbraco.Web/Routing/RoutingContext.cs index bb52a43193..b999e2241a 100644 --- a/src/Umbraco.Web/Routing/RoutingContext.cs +++ b/src/Umbraco.Web/Routing/RoutingContext.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Umbraco.Web.PublishedCache; @@ -9,6 +10,10 @@ namespace Umbraco.Web.Routing /// public class RoutingContext { + private readonly Lazy _urlProvider; + private readonly Lazy> _publishedContentFinders; + private readonly Lazy _publishedContentLastChanceFinder; + /// /// Initializes a new instance of the class. /// @@ -23,9 +28,21 @@ namespace Umbraco.Web.Routing UrlProvider urlProvider) { UmbracoContext = umbracoContext; - PublishedContentFinders = contentFinders; - PublishedContentLastChanceFinder = contentLastChanceFinder; - UrlProvider = urlProvider; + _publishedContentFinders = new Lazy>(() => contentFinders, false); + _publishedContentLastChanceFinder = new Lazy(() => contentLastChanceFinder, false); + _urlProvider = new Lazy(() => urlProvider, false); + } + + internal RoutingContext( + UmbracoContext umbracoContext, + Lazy> contentFinders, + Lazy contentLastChanceFinder, + Lazy urlProvider) + { + UmbracoContext = umbracoContext; + _publishedContentFinders = contentFinders; + _publishedContentLastChanceFinder = contentLastChanceFinder; + _urlProvider = urlProvider; } /// @@ -33,19 +50,28 @@ namespace Umbraco.Web.Routing /// public UmbracoContext UmbracoContext { get; private set; } - /// - /// Gets the published content finders. - /// - internal IEnumerable PublishedContentFinders { get; private set; } + /// + /// Gets the published content finders. + /// + internal IEnumerable PublishedContentFinders + { + get { return _publishedContentFinders.Value; } + } - /// - /// Gets the published content last chance finder. - /// - internal IContentFinder PublishedContentLastChanceFinder { get; private set; } + /// + /// Gets the published content last chance finder. + /// + internal IContentFinder PublishedContentLastChanceFinder + { + get { return _publishedContentLastChanceFinder.Value; } + } - /// - /// Gets the urls provider. - /// - public UrlProvider UrlProvider { get; private set; } + /// + /// Gets the urls provider. + /// + public UrlProvider UrlProvider + { + get { return _urlProvider.Value; } + } } } \ No newline at end of file diff --git a/src/Umbraco.Web/UmbracoContext.cs b/src/Umbraco.Web/UmbracoContext.cs index 47c55de9e0..ab9a9ae70e 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -31,6 +31,8 @@ namespace Umbraco.Web private static readonly object Locker = new object(); private bool _replacing; + private Lazy _contentCache; + private Lazy _mediaCache; /// /// Used if not running in a web application (no real HttpContext) @@ -94,21 +96,21 @@ namespace Umbraco.Web var umbracoContext = new UmbracoContext( httpContext, applicationContext, - PublishedCachesResolver.Current.Caches, + new Lazy(() => PublishedCachesResolver.Current.Caches, false), preview); - // create the nice urls provider - // there's one per request because there are some behavior parameters that can be changed - var urlProvider = new UrlProvider( - umbracoContext, - UrlProviderResolver.Current.Providers); - // create the RoutingContext, and assign var routingContext = new RoutingContext( umbracoContext, - ContentFinderResolver.Current.Finders, - ContentLastChanceFinderResolver.Current.Finder, - urlProvider); + new Lazy>(() => ContentFinderResolver.Current.Finders), + new Lazy(() => ContentLastChanceFinderResolver.Current.Finder), + // create the nice urls provider + // there's one per request because there are some behavior parameters that can be changed + new Lazy( + () => new UrlProvider( + umbracoContext, + UrlProviderResolver.Current.Providers), + false)); //assign the routing context back umbracoContext.RoutingContext = routingContext; @@ -118,6 +120,22 @@ namespace Umbraco.Web return UmbracoContext.Current; } + /// + /// Creates a new Umbraco context. + /// + /// + /// + /// The published caches. + /// An optional value overriding detection of preview mode. + internal UmbracoContext( + HttpContextBase httpContext, + ApplicationContext applicationContext, + IPublishedCaches publishedCaches, + bool? preview = null) + : this(httpContext, applicationContext, new Lazy(() => publishedCaches), preview) + { + } + /// /// Creates a new Umbraco context. /// @@ -128,7 +146,7 @@ namespace Umbraco.Web internal UmbracoContext( HttpContextBase httpContext, ApplicationContext applicationContext, - IPublishedCaches publishedCaches, + Lazy publishedCaches, bool? preview = null) { if (httpContext == null) throw new ArgumentNullException("httpContext"); @@ -141,8 +159,8 @@ namespace Umbraco.Web Application = applicationContext; Security = new WebSecurity(); - ContentCache = publishedCaches.CreateContextualContentCache(this); - MediaCache = publishedCaches.CreateContextualMediaCache(this); + _contentCache = new Lazy(() => publishedCaches.Value.CreateContextualContentCache(this)); + _mediaCache = new Lazy(() => publishedCaches.Value.CreateContextualMediaCache(this)); InPreviewMode = preview ?? DetectInPreviewModeFromRequest(); // set the urls... @@ -241,17 +259,23 @@ namespace Umbraco.Web /// /// Gets or sets the published content cache. /// - public ContextualPublishedContentCache ContentCache { get; private set; } + public ContextualPublishedContentCache ContentCache + { + get { return _contentCache.Value; } + } /// /// Gets or sets the published media cache. /// - public ContextualPublishedMediaCache MediaCache { get; private set; } + public ContextualPublishedMediaCache MediaCache + { + get { return _mediaCache.Value; } + } /// - /// Boolean value indicating whether the current request is a front-end umbraco request - /// - public bool IsFrontEndUmbracoRequest + /// Boolean value indicating whether the current request is a front-end umbraco request + /// + public bool IsFrontEndUmbracoRequest { get { return PublishedContentRequest != null; } }