diff --git a/src/Umbraco.Tests/MockTests.cs b/src/Umbraco.Tests/MockTests.cs index 45b07d4c71..c028992772 100644 --- a/src/Umbraco.Tests/MockTests.cs +++ b/src/Umbraco.Tests/MockTests.cs @@ -116,7 +116,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 198b3c85f3..96c52f7cb4 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -24,6 +24,8 @@ namespace Umbraco.Web private bool _replacing; private bool? _previewing; + private Lazy _contentCache; + private Lazy _mediaCache; /// /// Used if not running in a web application (no real HttpContext) @@ -175,22 +177,22 @@ namespace Umbraco.Web var umbracoContext = new UmbracoContext( httpContext, applicationContext, - PublishedCachesResolver.Current.Caches, + new Lazy(() => PublishedCachesResolver.Current.Caches, false), webSecurity, 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; @@ -214,6 +216,22 @@ namespace Umbraco.Web IPublishedCaches publishedCaches, WebSecurity webSecurity, bool? preview = null) + : this(httpContext, applicationContext, new Lazy(() => publishedCaches), preview) + { + } + + /// + /// Creates a new Umbraco context. + /// + /// + /// + /// The published caches. + /// An optional value overriding detection of preview mode. + internal UmbracoContext( + HttpContextBase httpContext, + ApplicationContext applicationContext, + Lazy publishedCaches, + bool? preview = null) { //This ensures the dispose method is called when the request terminates, though // we also ensure this happens in the Umbraco module because the UmbracoContext is added to the @@ -230,8 +248,8 @@ namespace Umbraco.Web Application = applicationContext; Security = webSecurity; - ContentCache = publishedCaches.CreateContextualContentCache(this); - MediaCache = publishedCaches.CreateContextualMediaCache(this); + _contentCache = new Lazy(() => publishedCaches.Value.CreateContextualContentCache(this)); + _mediaCache = new Lazy(() => publishedCaches.Value.CreateContextualMediaCache(this)); _previewing = preview; // set the urls... @@ -331,17 +349,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; } }