I am working with Umbraco 4.7.1, and I am trying to map content nodes to some auto-generated strongly typed objects. I tried using both the valueinjecter and automapper methods, but OOTB none of them display my properties. I think this is because all the properties on the Umbraco node (cms document) are retrieved as follows:
node.GetProperty("propertyName").Value;
And my strongly typed objects are in the format MyObject.PropertyName. So, how do I map a property on a node that is retrieved using a method and a line starting with a lowercase character to a property in MyObject, where the property starts with an uppercase character?
UPDATE
I was able to create the following code that displays the umbraco node, as expected, by digging in the Umbraco source code for some inspiration on how to distinguish string properties from strongly typed properties:
public class UmbracoInjection : SmartConventionInjection
{
protected override bool Match(SmartConventionInfo c)
{
return c.SourceProp.Name == c.TargetProp.Name;
}
protected override void Inject(object source, object target)
{
if (source != null && target != null)
{
Node node = source as Node;
var props = target.GetProps();
var properties = node.Properties;
for (int i = 0; i < props.Count; i++)
{
var targetProperty = props[i];
var sourceProperty = properties[targetProperty.Name];
if (sourceProperty != null && !string.IsNullOrWhiteSpace(sourceProperty.Value))
{
var value = sourceProperty.Value;
var type = targetProperty.PropertyType;
if (targetProperty.PropertyType.IsValueType && targetProperty.PropertyType.GetGenericArguments().Length > 0 && typeof(Nullable<>).IsAssignableFrom(targetProperty.PropertyType.GetGenericTypeDefinition()))
{
type = type.GetGenericArguments()[0];
}
targetProperty.SetValue(target, Convert.ChangeType(value, type));
}
}
}
}
}
As you can see, I'm using SmartConventionInjection to speed things up. It takes approximately 20 seconds to display approximately 16,000 objects. Can this be done even faster?
thank
Thomas
source
share