Monday, July 26, 2010

How to cast objects between real data types and proxy data types

When you use web or WCF services your real data types are recreated into new proxy data types.  For example if you have a class called myNamespace.Person and consume that data type in a client using a service reference you get a new object xxxx.Person that while it has the same structure (properties) you can not do things like this;
dim person as new myNamespace.person
dim person2 as new servicereference1.person
person = person2
This can be an issue and while there are options such as hard coding the mapping it your self or using reflection to map the object, there is a better way.
AutoMapper is an object-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type. What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B. As long as type B follows AutoMapper's established convention, almost zero configuration is needed to map two types.
This is a nice option when you need to move an object between the two types.

No comments: