AutoMapper is a convention-based object-object mapper as described by the AutoMapper project homepage (http://www.codeplex.com/AutoMapper).

Basically it will map properties between two different objects (different types). If the name of the properties are the same it just works. It works with lists and nested types. All you need to do is initiate the mapping and then do the actual mapping. Here’s an example when mapping from an instance of type User to a UserViewModel instance.

   1: Mapper.CreateMap<User, UserViewModel>;
   2: User user = new UserRepository().Get(1);
   3: UserViewModel userViewModel = Mapper.Map<User, UserViewModel>();

If the structure/name isn’t something that AutoMapper can figure out how to map then it’s possible to add manual mappings with lambda expressions. For example if the User has a Telephone type with a property called PhoneNumber and the UserViewModel has a basic string property called Phone:

   1: Mapper.CreateMap<User, UserViewModel>()
   2:     .ForMember(viewModel => viewModel.Phone, opt => opt.MapFrom(user => user.Telephone.PhoneNumber)));
   3:  
   4:  

Very cool stuff indeed. I introduced AutoMapper in one of the projects I’m currently working on and since it was mostly 1-1 mapping that was done, I reduced over 600 lines of code in two hours in the project. Since I just replaced the body of my mapper methods with the call to AutoMapper that meant that all the unit tests I had written could verify that everything worked as before.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Anonymous types

Posted on February 28 by bjn

While reading Jon Skeets book C# in Depth I noticed he mentioned that when using anonymous types it's important that the name and type are the same for the properties. As well as the order. Since the underlying type is using generics it will generate two types when the order of the properties are reversed.

For example, take a look at the following C# code:

var idName = new { Name = "Volvo", Id = 28 };
var idName2 = new { Name = "SAAB", Id = 29 };


What's actually generated under the hood by the compiler looks like this in IL code:

   1: L_0001: ldstr "Volvo"
   2: L_0006: ldc.i4.s 0x1c
   3: L_0008: newobj instance void <>f__AnonymousType4`2<string, int32>::.ctor(!0, !1)
   4: L_000d: stloc.0 
   5: L_000e: ldstr "SAAB"
   6: L_0013: ldc.i4.s 0x1d
   7: L_0015: newobj instance void <>f__AnonymousType4`2<string, int32>::.ctor(!0, !1)
   8: L_001a: stloc.1 


As you can see the type is constructed with the use of generics, where the order of the properties are used. In this example the type will be created with <string, int32>, and both of the objects will be of the same type.

However, if the properties are reversed:

var idName = new { Name = "Volvo", Id = 28 };
var nameId = new { Id = 29, Name = "SAAB" };


The IL code generated is changed:

   1: L_0001: ldstr "Volvo"
   2: L_0006: ldc.i4.s 0x1c
   3: L_0008: newobj instance void <>f__AnonymousType4`2<string, int32>::.ctor(!0, !1)
   4: L_000d: stloc.0 
   5: L_000e: ldc.i4.s 0x1c
   6: L_0010: ldstr "SAAB"
   7: L_0015: newobj instance void <>f__AnonymousType5`2<int32, string>::.ctor(!0, !1)
   8: L_001a: stloc.1 

This time, a new type is generated for the second object where generic initialization is reversed to <int32, string>.

Types are re-used by the compiler within one assembly, but within one assembly it's wise to make sure that the properties are in the same order. Thanks to Resharper life's much easier with their detection of this kind of problems. If you're trying to initialize an anonymous type with the properties reveresed Resharper (I'm using v4.1) pops up and says: "Similar anonymous type detected nearby. Are they the same?"

anontype

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5