如何自动映射.NET函数调用的财产? [英] How to map .NET function call to property automatically?

查看:150
本文介绍了如何自动映射.NET函数调用的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这原本是1:1的映射,但我想通了,我需要一个更复杂的递归映射使一个新的问题被张贴在这里:<一href=\"http://stackoverflow.com/questions/20573600/how-to-recursively-map-entity-to-view-model-with-automapper-function-call\">How递归映射实体查看模型Automapper函数调用?

This was originally for 1:1 mapping, but I figured out that I needed a more complicated recursive mapping so a new question was posted here: How to recursively map entity to view model with Automapper function call?

我想一个实体类的功能一般映射到一个视图模型,采用ServiceStack的ConvertTo&LT;>方法。这将映射所有类似的类型和属性名称和工作正常,但我想找到一种方法来映射功能属性的结果。下面是一些code

I am trying to generically map an entity class function to a view model, using ServiceStack ConvertTo<> method. This maps all similar types and property names and works fine, but I want to find a way to map the result of a function to a property. Here is some code

实体例如:

public class Item {
    public long Id {get; set;}
    public List<Product> GetProducts() {
          return Repository.GetAll<Product>();
    }
}

视图模型的例子:

ViewModel example:

 public class ItemViewModel {
   public long Id {get;set;}
   public List<Product> Products {get; set;}
 }

理想的结果将是有一个地图功能,着眼于实体类的相匹配​​的返回类型和函数名的方法获取+物业的名称,然后执行它,并将结果映射到视图模型。

The ideal result would be to have a map function that looks at the entity class for a method that matches the return type and the function name is "Get" + Property name, then to execute it and map the result to the view model.

推荐答案

虽然你也许能找到类似的 Automapper 价值喷油器可能已经包含这个功能,有从写一个小巧的阻止你什么实用方法:

While you might be able to find that something like Automapper or Value Injecter could already contain this functionality, there's nothing from stopping you from writing a quick little utility method:

public TTarget MapToViewModel<TSource, TTarget>(TSource source)
    where TTarget : new()
{
    var sourceType = source.GetType();

    var targetProperties = typeof(TTarget).GetProperties(BindingFlags.Public | BindingFlags.Instance);

    var sourceProperties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
    var sourceMethods = sourceType.GetMethods(BindingFlags.Public | BindingFlags.Instance);

    var target = Activator.CreateInstance<TTarget>();

    foreach (var prop in targetProperties)
    {
        var sourceProp = sourceProperties.FirstOrDefault(x => x.Name == prop.Name);
        if (sourceProp != null)
        {
            prop.SetValue(target, sourceProp.GetValue(source, null), null);
        }
        else
        {
            var sourceMethod = sourceMethods.FirstOrDefault(x => x.Name == "Get" + prop.Name);
            if (sourceMethod != null)
            {
                prop.SetValue(target, sourceMethod.Invoke(source, null), null);
            }
        }
    }

    return target;
}

在条件 .FirstOrDefault 调用上面只使用名称,但你可以将它们扩展到任何你喜欢

The conditions in the .FirstOrDefault calls above just use Name, but you could expand them to whatever you like.

要使用它,只是把它像这样:

To use this, just call it like so:

var itemViewModel = MapToViewModel<Item, ItemViewModel>(item);

只是要小心,如果你在你的常规模式为方法的参数。如果你发现你的模型过于复杂(或不按一个或两个命名约定),那么你可能会喜欢的东西AutoMapper更好。对于简单的映射,不过,这样的事情应该做的工作。

Just be careful if you have any parameters for your methods in your regular model. If you find that your models are too complex (or don't follow one or two naming conventions), then you might be better off with something like AutoMapper. For simple mappings, though, something like this should do the job.

这篇关于如何自动映射.NET函数调用的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆