AutoMapper - Map using the same source and destination object types

ε祈祈猫儿з 提交于 2019-12-12 10:35:44

问题


I'm using Automapper to take two objects of the same type and map any new values that have changed. 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.

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. 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. There might even be a better solution - any help would be appreciated!


回答1:


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?




回答2:


This can be done using tuples and by creating a custom type converter deriving from Automapper's Abstract TypeConverter class.

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



回答3:


This is a known behavior of Automapper (see issue). You actually have to tell Automapper:

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


来源:https://stackoverflow.com/questions/9980344/automapper-map-using-the-same-source-and-destination-object-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!