Added convenience overload taking a default value factory method.

This overload is useful in scenarios where you want to provide a default value but is relatively expensive to execute.
Using the overload, the factory method is only executed if the infoset does not contain an entry for the requested key.
This commit is contained in:
Sipke Schoorstra
2015-08-23 22:40:34 +01:00
parent b560a4c3d0
commit 7fae246341

View File

@@ -10,19 +10,26 @@ namespace Orchard.ContentManagement {
public static TProperty Retrieve<TPart, TProperty>(this TPart contentPart,
Expression<Func<TPart, TProperty>> targetExpression,
TProperty defaultValue = default(TProperty),
Func<TProperty> defaultValue,
bool versioned = false) where TPart : ContentPart {
var propertyInfo = ReflectionHelper<TPart>.GetPropertyInfo(targetExpression);
var name = propertyInfo.Name;
var infosetPart = contentPart.As<InfosetPart>();
var el = infosetPart == null
? null
: (versioned ? infosetPart.VersionInfoset.Element : infosetPart.Infoset.Element)
.Element(contentPart.GetType().Name);
var attr = el != null ? el.Attribute(name) : default(XAttribute);
return attr == null ? defaultValue : XmlHelper.Parse<TProperty>(attr.Value);
return attr == null ? defaultValue != null ? defaultValue() : default(TProperty) : XmlHelper.Parse<TProperty>(attr.Value);
}
public static TProperty Retrieve<TPart, TProperty>(this TPart contentPart,
Expression<Func<TPart, TProperty>> targetExpression,
TProperty defaultValue = default(TProperty),
bool versioned = false) where TPart : ContentPart {
return Retrieve(contentPart, targetExpression, () => defaultValue, versioned);
}
public static TProperty Retrieve<TProperty>(this ContentPart contentPart, string name,