【问题标题】:Mapping umbraco node to strongtyped object将 umbraco 节点映射到强类型对象
【发布时间】:2012-04-18 07:06:21
【问题描述】:

我正在使用 Umbraco 4.7.1,我正在尝试将内容节点映射到一些自动生成的强类型对象。我曾尝试同时使用 valueinjecter 和 automapper,但 OOTB 它们都没有映射我的属性。我猜这是因为 Umbraco 节点(cms 文档)上的所有属性都是这样检索的:

node.GetProperty("propertyName").Value;

而且我的强类型对象的格式是 MyObject.PropertyName。那么如何将使用方法和以小写字符开头的字符串检索到的节点上的属性映射到 MyObject 上的属性以大写字符开头的属性?

更新 通过在 Umbraco 源代码中四处挖掘以获取有关如何将字符串属性转换为强类型属性的一些灵感,我设法创建了以下代码,该代码按预期映射了 umbraco 节点:

    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));
                }
            }
        }
    }
}

如您所见,我使用 SmartConventionInjection 来加快速度。 映射 16000 个对象仍然需要大约 20 秒。这可以更快地完成吗?

谢谢

托马斯

【问题讨论】:

    标签: automapper umbraco valueinjecter


    【解决方案1】:

    使用 ValueInjecter,您可以执行以下操作:

    public class Um : ValueInjection
    {
        protected override void Inject(object source, object target)
        {
            var node = target as Node;
            var props = source.GetProps();
            for (int i = 0; i < props.Count; i++)
            {
                var prop = props[i];
                target.GetProperty(prop.Name).Value;
    
            }
        }
    }
    

    【讨论】:

    • 谢谢。当我尝试在目标上设置值时,出现错误:“System.String”类型的对象无法转换为“System.Nullable`1[System.Int32] 类型。任何帮助表示赞赏。谢谢
    • 我没有发布整个代码,但基本上该消息表明您正在尝试使用 int 设置字符串属性?值,您可以通过执行 target.GetProperty(prop.Name) 看到我仅按名称匹配属性,但我不检查类型或任何内容,因此如果您有相同的道具,则需要这样做名称不同的类型
    • 嗨。属性的名称是唯一的,源节点上的所有值都存储为字符串。如何将我的源属性转换为正确的目标属性类型?我对所有这些反思的东西都很陌生,很抱歉这可能是一个简单的问题
    • 这很容易,你应该用intellisense弄清楚,我不知道umbraco的东西,但是对于普通对象你可以这样做var sourceValue = prop.GetValue(source);
    • 谢谢。我无法让它工作。问题是 Umbraco 值都是字符串。但是我试图将属性映射到的自动生成的对象是不同的类型。例如。我可能有一个像 Node.Price = "54" 这样的属性,并且在强类型对象 Object.Price 上是一个 int。是否可以从强类型对象中推断出该类型,然后在将其分配给强类型对象之前从节点转换字符串值?谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多