Injecting Umbraco Context Accessor and not saving the url on the class

This commit is contained in:
Bjarke Berg
2019-01-23 14:16:42 +01:00
parent fedf0c78de
commit f8b52a57c3
24 changed files with 194 additions and 100 deletions

View File

@@ -15,7 +15,12 @@ namespace Umbraco.Web.Models
[DebuggerDisplay("Content Id: {Id}, Name: {Name}")]
public abstract class PublishedContentBase : IPublishedContent
{
private string _url; // fixme/task - cannot cache urls, they depends on the current request
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
protected PublishedContentBase(IUmbracoContextAccessor umbracoContextAccessor)
{
_umbracoContextAccessor = umbracoContextAccessor;
}
#region ContentType
@@ -81,24 +86,21 @@ namespace Umbraco.Web.Models
/// </remarks>
public virtual string GetUrl(string culture = null) // todo - consider .GetCulture("fr-FR").Url
{
var umbracoContext = _umbracoContextAccessor.UmbracoContext;
switch (ItemType)
{
case PublishedItemType.Content:
// todo - consider injecting an umbraco context accessor
if (UmbracoContext.Current == null)
throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext.Current is null.");
if (UmbracoContext.Current.UrlProvider == null)
throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext.Current.UrlProvider is null.");
return UmbracoContext.Current.UrlProvider.GetUrl(this, culture);
if (umbracoContext == null)
throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext is null.");
if (umbracoContext.UrlProvider == null)
throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext.UrlProvider is null.");
return umbracoContext.UrlProvider.GetUrl(this, culture);
case PublishedItemType.Media:
if (_url != null) return _url; // assume it will not depend on current uri/culture
var prop = GetProperty(Constants.Conventions.Media.File);
if (prop?.GetValue() == null)
{
_url = string.Empty;
return _url;
return string.Empty;
}
var propType = ContentType.GetPropertyType(Constants.Conventions.Media.File);
@@ -110,7 +112,7 @@ namespace Umbraco.Web.Models
switch (propType.EditorAlias)
{
case Constants.PropertyEditors.Aliases.UploadField:
_url = prop.GetValue().ToString();
return prop.GetValue().ToString();
break;
case Constants.PropertyEditors.Aliases.ImageCropper:
//get the url from the json format
@@ -118,14 +120,12 @@ namespace Umbraco.Web.Models
var stronglyTyped = prop.GetValue() as ImageCropperValue;
if (stronglyTyped != null)
{
_url = stronglyTyped.Src;
break;
return stronglyTyped.Src;
}
_url = prop.GetValue()?.ToString();
break;
return prop.GetValue()?.ToString();
}
return _url;
return string.Empty;
default:
throw new NotSupportedException();