Files
Umbraco-CMS/src/Umbraco.Core/Models/PublishedContent/PublishedContentExtended.cs

94 lines
3.1 KiB
C#
Raw Normal View History

2017-05-12 14:49:44 +02:00
using System.Collections.Generic;
2013-11-07 17:16:22 +01:00
using System.Collections.ObjectModel;
2017-05-12 14:49:44 +02:00
using System.ComponentModel;
2013-11-07 17:16:22 +01:00
using System.Linq;
namespace Umbraco.Core.Models.PublishedContent
{
2017-05-12 14:49:44 +02:00
[TypeConverter(typeof(PublishedContentTypeConverter))]
2013-11-07 17:16:22 +01:00
public class PublishedContentExtended : PublishedContentWrapped, IPublishedContentExtended
{
#region Constructor
// protected for models, private for our static Extend method
protected PublishedContentExtended(IPublishedContent content)
: base(content)
{ }
#endregion
#region Extend
private static IPublishedContent Unwrap(IPublishedContent content)
2013-11-07 17:16:22 +01:00
{
if (content == null) return null;
2014-07-24 11:59:37 +02:00
while (true)
{
var extended = content as PublishedContentExtended;
if (extended != null)
{
if (((IPublishedContentExtended)extended).HasAddedProperties) return extended;
content = extended.Unwrap();
continue;
}
var wrapped = content as PublishedContentWrapped;
if (wrapped != null)
{
content = wrapped.Unwrap();
continue;
}
return content;
}
}
2013-11-07 17:16:22 +01:00
internal static IPublishedContentExtended Extend(IPublishedContent content)
{
// first unwrap content down to the lowest possible level, ie either the deepest inner
// IPublishedContent or the first extended that has added properties. this is to avoid
// nesting extended objects as much as possible, so we try to re-extend that lowest
// object. Then extend. But do NOT create a model, else we would not be able to add
// properties. BEWARE means that whatever calls Extend MUST create the model.
2014-07-24 11:59:37 +02:00
return new PublishedContentExtended(Unwrap(content));
2013-11-07 17:16:22 +01:00
}
#endregion
#region IPublishedContentExtended
void IPublishedContentExtended.AddProperty(IPublishedProperty property)
{
if (_properties == null)
_properties = new Collection<IPublishedProperty>();
_properties.Add(property);
}
bool IPublishedContentExtended.HasAddedProperties => _properties != null;
2013-11-07 17:16:22 +01:00
#endregion
#region Properties
private ICollection<IPublishedProperty> _properties;
public override IEnumerable<IPublishedProperty> Properties => _properties == null
? Content.Properties
: Content.Properties.Union(_properties).ToList();
2013-11-07 17:16:22 +01:00
public override IPublishedProperty GetProperty(string alias)
{
return _properties == null
? Content.GetProperty(alias)
: _properties.FirstOrDefault(prop => prop.PropertyTypeAlias.InvariantEquals(alias)) ?? Content.GetProperty(alias);
}
#endregion
2017-05-12 14:49:44 +02:00
public override string ToString()
{
return Id.ToString();
}
}
2013-11-07 17:16:22 +01:00
}