updates some ctors on umbraco context and routing context to have lazy overloads which allows us to more easily create/mock the umbraco context.

This commit is contained in:
Shannon
2014-03-03 04:16:09 +01:00
parent 651182f5de
commit feb9f40237
3 changed files with 83 additions and 34 deletions

View File

@@ -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()
{

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Umbraco.Web.PublishedCache;
@@ -9,6 +10,10 @@ namespace Umbraco.Web.Routing
/// </summary>
public class RoutingContext
{
private readonly Lazy<UrlProvider> _urlProvider;
private readonly Lazy<IEnumerable<IContentFinder>> _publishedContentFinders;
private readonly Lazy<IContentFinder> _publishedContentLastChanceFinder;
/// <summary>
/// Initializes a new instance of the <see cref="RoutingContext"/> class.
/// </summary>
@@ -23,9 +28,21 @@ namespace Umbraco.Web.Routing
UrlProvider urlProvider)
{
UmbracoContext = umbracoContext;
PublishedContentFinders = contentFinders;
PublishedContentLastChanceFinder = contentLastChanceFinder;
UrlProvider = urlProvider;
_publishedContentFinders = new Lazy<IEnumerable<IContentFinder>>(() => contentFinders, false);
_publishedContentLastChanceFinder = new Lazy<IContentFinder>(() => contentLastChanceFinder, false);
_urlProvider = new Lazy<UrlProvider>(() => urlProvider, false);
}
internal RoutingContext(
UmbracoContext umbracoContext,
Lazy<IEnumerable<IContentFinder>> contentFinders,
Lazy<IContentFinder> contentLastChanceFinder,
Lazy<UrlProvider> urlProvider)
{
UmbracoContext = umbracoContext;
_publishedContentFinders = contentFinders;
_publishedContentLastChanceFinder = contentLastChanceFinder;
_urlProvider = urlProvider;
}
/// <summary>
@@ -33,19 +50,28 @@ namespace Umbraco.Web.Routing
/// </summary>
public UmbracoContext UmbracoContext { get; private set; }
/// <summary>
/// Gets the published content finders.
/// </summary>
internal IEnumerable<IContentFinder> PublishedContentFinders { get; private set; }
/// <summary>
/// Gets the published content finders.
/// </summary>
internal IEnumerable<IContentFinder> PublishedContentFinders
{
get { return _publishedContentFinders.Value; }
}
/// <summary>
/// Gets the published content last chance finder.
/// </summary>
internal IContentFinder PublishedContentLastChanceFinder { get; private set; }
/// <summary>
/// Gets the published content last chance finder.
/// </summary>
internal IContentFinder PublishedContentLastChanceFinder
{
get { return _publishedContentLastChanceFinder.Value; }
}
/// <summary>
/// Gets the urls provider.
/// </summary>
public UrlProvider UrlProvider { get; private set; }
/// <summary>
/// Gets the urls provider.
/// </summary>
public UrlProvider UrlProvider
{
get { return _urlProvider.Value; }
}
}
}

View File

@@ -31,6 +31,8 @@ namespace Umbraco.Web
private static readonly object Locker = new object();
private bool _replacing;
private Lazy<ContextualPublishedContentCache> _contentCache;
private Lazy<ContextualPublishedMediaCache> _mediaCache;
/// <summary>
/// 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<IPublishedCaches>(() => 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<IEnumerable<IContentFinder>>(() => ContentFinderResolver.Current.Finders),
new Lazy<IContentFinder>(() => 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<UrlProvider>(
() => 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;
}
/// <summary>
/// Creates a new Umbraco context.
/// </summary>
/// <param name="httpContext"></param>
/// <param name="applicationContext"> </param>
/// <param name="publishedCaches">The published caches.</param>
/// <param name="preview">An optional value overriding detection of preview mode.</param>
internal UmbracoContext(
HttpContextBase httpContext,
ApplicationContext applicationContext,
IPublishedCaches publishedCaches,
bool? preview = null)
: this(httpContext, applicationContext, new Lazy<IPublishedCaches>(() => publishedCaches), preview)
{
}
/// <summary>
/// Creates a new Umbraco context.
/// </summary>
@@ -128,7 +146,7 @@ namespace Umbraco.Web
internal UmbracoContext(
HttpContextBase httpContext,
ApplicationContext applicationContext,
IPublishedCaches publishedCaches,
Lazy<IPublishedCaches> 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<ContextualPublishedContentCache>(() => publishedCaches.Value.CreateContextualContentCache(this));
_mediaCache = new Lazy<ContextualPublishedMediaCache>(() => publishedCaches.Value.CreateContextualMediaCache(this));
InPreviewMode = preview ?? DetectInPreviewModeFromRequest();
// set the urls...
@@ -241,17 +259,23 @@ namespace Umbraco.Web
/// <summary>
/// Gets or sets the published content cache.
/// </summary>
public ContextualPublishedContentCache ContentCache { get; private set; }
public ContextualPublishedContentCache ContentCache
{
get { return _contentCache.Value; }
}
/// <summary>
/// Gets or sets the published media cache.
/// </summary>
public ContextualPublishedMediaCache MediaCache { get; private set; }
public ContextualPublishedMediaCache MediaCache
{
get { return _mediaCache.Value; }
}
/// <summary>
/// Boolean value indicating whether the current request is a front-end umbraco request
/// </summary>
public bool IsFrontEndUmbracoRequest
/// Boolean value indicating whether the current request is a front-end umbraco request
/// </summary>
public bool IsFrontEndUmbracoRequest
{
get { return PublishedContentRequest != null; }
}