tags:

views:

38

answers:

1

Hi,

I am accessing a service and I get returned an object in the form of (for example)

Car _car = _service.FetchCar(carId)

    Car.Color
    Car.Tires.Right.Front
    Car.Tires.Left.Front
    Car.Tires.Right.Back
    Car.Tires.Left.Back
    Car.Spoiler

etc, etc...you get the idea. My application is recieving many different objects with many differen structures. What I'd like to do is to be able to have one method that would be able to take one type of object and map it to another...

What I don't want to have to do is to manually map all the fields from the service object to my domain object with every object type

for example

If I get a Car object from the service I'd like to map it to my own Car object and if I get a Table object I'd like to map it to my own table object

any ideas?

A: 

Have a look at tools like AutoMapper to handle these "copy all fields from object A to object B" scenarios.

AutoMapper will automatically copy all fields with identical names from one instance to the other, and you can set up additional rules to allow copying of fields where the names don't match (and you can also define custom converters if you need to convert data types along the way).

Very useful, very helpful!

Marc

marc_s
thanks. I have looked at AutoMapper but I don't understand it's Mapper.Initialize call - what am I supposed to put here?
kurasa
never mind...got it sorted. Came across the following article and the penny droppedhttp://mhinze.com/automapper-in-nerddinner/
kurasa