johndoe
johndoe

Reputation: 65

AutoMapper Convention Based System

I am trying to map my ViewModel classes to web service DTO objects. The ViewModels uses the following convention:

CustomerViewModel 
OrderStatusViewModel 

The WCF classes DTO has the following convention:

Customer
OrderStatus 

Also, what if the WCF classes DTO has the following convention:

CustomerDTO 
OrderStatusDTO 

The question is how do I map between the ViewModel and the WCF classes using AutoMapper? I want to map in a certain way that all future ViewModels and WCF classes are mapped automatically due to the above configuration.

Upvotes: 2

Views: 1394

Answers (2)

Evan Nagle
Evan Nagle

Reputation: 5133

I wrote about this sometime back. Check 'er out if you'd like: http://www.weirdlover.com/2010/07/01/the-big-boy-mvc-series-part-22-whoop/

Add a reference to Automapper

Create a base ViewModel class:

public abstract class MappedTo<T>
{
    public MappedTo()
    {
        Mapper.CreateMap(GetType(), typeof(T));
    }

    public T Convert()
    {
        return (T)Mapper.Map(this, this.GetType(), typeof(T));
    }
}

Create a ViewModel class that inherits from the aforementioned base. Specify which DTO you'd like to map your ViewModel to:

class AddressViewModel : MappedTo<Address>
{
    public string Line1 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

AutoMapper should handle the rest:

static void Main(string[] args)
{
    AddressViewModel addressVm = new AddressViewModel
    {
        Line1 = "555 Honolulu Street",
        City = "Honolulu",
        State = "HI"
    };

    Address address = addressVm.Convert();

    Console.WriteLine(address.Line1);
    Console.WriteLine(address.City);
    Console.WriteLine(address.State);
    Console.ReadLine();
}

If you want to get fancy, you can create another ViewModel base calss that allows you to pass in your own TypeConverter:

public abstract class MappedTo<T, TConverter>
{
    public MappedTo()
    {
        Mapper.CreateMap(GetType(), typeof(T)).ConvertUsing(typeof(TConverter));
    }

    public T Convert()
    {
        return (T)Mapper.Map(this, this.GetType(), typeof(T));
    }
}

Then, you can convert from your ViewModel to your DTO however you see fit:

public class AddressConverter : TypeConverter<AddressViewModel, Address>
{
    protected override Address ConvertCore(AddressViewModel source)
    {
        return new Address
        {
            Line1 = source.Line1 + " foo",
            City = source.City + " foo",
            State = source.State + " foo"
        };
    }
}

Upvotes: 3

Emond
Emond

Reputation: 50692

You could have a look at this: mvmmapper.codeplex.com I wrote a tool for Visual Studio that generates the mapping for you.

Read the examples to get an idea of what the tool does and then decide to write your own tool or use an existing one...

Edit: Have a look at this code it is from the MVMMapper and being generated. It might give you an idea.

Upvotes: 0

Related Questions