简体   繁体   English

AutoMapper - 使用相同的源和目标对象类型映射

[英]AutoMapper - Map using the same source and destination object types

I'm using Automapper to take two objects of the same type and map any new values that have changed. 我正在使用Automapper来获取两个相同类型的对象,并映射已更改的任何新值。 I tried using the code below, but it keeps throwing an error and I'm not even sure if this can even be achieved with Automapper. 我尝试使用下面的代码,但它不断抛出错误,我甚至不确定这是否可以通过Automapper实现。

For example: 例如:

        Mapper.CreateMap<UserDetails, UserDetails>();
        UserDetails userDetails = Mapper.Map<UserDetails, UserDetails>(userDetailsCurrent, userDetailsNew);

Basically, I need to copy across any new values that come in from the new object "userDetailsNew" to the existing object "userDetailsCurrent" - even though they are of the same type. 基本上,我需要将从新对象“userDetailsNew”进入的任何新值复制到现有对象“userDetailsCurrent” - 即使它们属于同一类型。 This way I can "update" the existing object with the new values. 这样我就可以用新值“更新”现有对象。 The reason I am doing this is because I am not sure what user details will be passed in - I need to map them as and when they arrive. 我这样做的原因是因为我不确定将传递哪些用户详细信息 - 我需要在它们到达时映射它们。

I have normally used Automapper to map different objects with similar properties - but I thought that I could use the power of Automapper to achieve the same thing this way. 我通常使用Automapper来映射具有相似属性的不同对象 - 但我认为我可以使用Automapper的强大功能以这种方式实现相同的功能。 There might even be a better solution - any help would be appreciated! 甚至可能有更好的解决方案 - 任何帮助将不胜感激!

This seems to work for me. 这似乎对我有用。 My custom type: 我的自定义类型:

class MyType
{
    public int MyInt { get; set; }
    public string MyString { get; set; }
}

My mapping code: 我的映射代码:

Mapper.CreateMap<MyType, MyType>();
var source = new MyType() {MyInt = 1, MyString = "Hello world"};
var dest = Mapper.Map<MyType, MyType>(source);

What is interesting about your custom type beyond simple properties? 除了简单属性之外,您的自定义类型有什么意义?

This can be done using tuples and by creating a custom type converter deriving from Automapper's Abstract TypeConverter class. 这可以使用元组和创建从Automapper的Abstract TypeConverter类派生的自定义类型转换器来完成。

Say you had a source and destination class of: 假设您有源和目标类:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override string ToString()
    {
        return string.Format("Firstname: {0}, Lastname: {1}", FirstName, LastName);
    }
}

Then you could build the custom converter type as 然后你可以构建自定义转换器类型

public class CustomerPersonConverter : TypeConverter<Tuple<Person, Person>, Person>
{
    protected override Person ConvertCore(Tuple<Person, Person> source)
    {
        var orginalValues = source.Item1;
        var updatedValues = source.Item2;

        var result = new Person
            {
                FirstName = string.IsNullOrEmpty(updatedValues.FirstName) ? orginalValues.FirstName : updatedValues.FirstName,
                LastName = string.IsNullOrEmpty(updatedValues.LastName) ? orginalValues.LastName : updatedValues.LastName
            };

        return result;
    }
}

which could be used like 可以像

var orginal = new Person() {FirstName = "Clifford", LastName = "Mayson"};
        var updated = new Person() {FirstName = "Cliff"};

        Mapper.CreateMap<Tuple<Person, Person>, Person>().ConvertUsing<CustomerPersonConverter>();

        var result = Mapper.Map<Person>(new Tuple<Person, Person>(orginal, updated));

        Console.WriteLine(result);

Which would produce the result of keeping the original last name value as that was missing in the update but updating the firstname value eg. 这将产生保持原始姓氏值的结果,因为更新中缺少原始姓氏值,但更新名字值,例如。

Firstname: Cliff, Lastname: Mayson

This is a known behavior of Automapper ( see issue ). 这是Automapper的已知行为( 请参阅问题 )。 You actually have to tell Automapper: 你实际上必须告诉Automapper:

 CreateMap<A,A>(); CreateMap<B,B>(); CreateMap<C,C>(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AutoMapper-将两个不同类型的源集合映射到一个目标集合中 - AutoMapper - Map two source collections of different types into a single destination collection 如何使用AutoMapper将目标对象与源对象中的子对象进行映射? - How to use AutoMapper to map destination object with a child object in the source object? AutoMapper将源对象上的单个列表映射到目标对象上的两个列表 - AutoMapper Map single list on source object into two lists on destination object 自动映射器,将Source映射到Destination的属性 - Automapper, map Source to property of Destination 使用 AutoMapper Map function 映射对象时,将源 object 的属性保留到目标属性 - Keep property of source object to destination property when mapping objects using AutoMapper Map function Automapper - 如何从源子对象映射到目标 - Automapper - How to map from source child object to destination AutoMapper不会将源映射到目标 - AutoMapper doesn't map source to a destination automapper 将每个 map 源和目标添加到字典 - automapper add every map source and destination to dictionary AutoMapper - Map 源列表到目标数组 - AutoMapper - Map source list to a destination array AutoMapper:如果指定类型的源对象为null,则将目标对象的所有属性设置为默认值 - AutoMapper: Set all properties of destination object to default if source object is null for specified types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM