Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper - Map using the same source and destination object types

Tags:

c#

automapper

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!

like image 584
Deano Avatar asked Apr 02 '12 16:04

Deano


3 Answers

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

CreateMap<A,A>();
CreateMap<B,B>();
CreateMap<C,C>();
like image 177
Jared Beach Avatar answered Oct 20 '22 05:10

Jared Beach


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
like image 41
Cliff Mayson Avatar answered Oct 20 '22 06:10

Cliff Mayson


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?

like image 10
Chris Farmer Avatar answered Oct 20 '22 04:10

Chris Farmer