Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Profiles in Automapper to map the same types with different logic

Tags:

c#

automapper

I am using AutoMapper in my ASP.NET MVC website to map my database objects to ViewModel objects and I am trying to use several profiles to map the same types, but using another logic. I had the idea of doing so by reading Matt's blog post where he says:

The really key part is the AutoMapper configuration profile. You can group configurations with profiles. Maybe in one profile you format dates in one way, in another profile you format dates in another way. I’m just using one profile here.

So I created a profile for one case:

public class MyProfile : Profile {     protected override string ProfileName     {         get         {             return "MyProfile";         }     }      protected override void Configure()     {         CreateMap<DateTime, String>().ConvertUsing<StringFromDateTimeTypeConverter>();     } }  public class StringFromDateTimeTypeConverter : ITypeConverter<DateTime, String> {     public string Convert(DateTime source)     {         return source.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture);     } } 

And another one for another case:

public class MyProfile2 : Profile {     protected override string ProfileName     {         get         {             return "MyProfile2";         }     }      protected override void Configure()     {         CreateMap<DateTime, String>().ConvertUsing<AnotherStringFromDateTimeTypeConverter>();     } }  public class AnotherStringFromDateTimeTypeConverter : ITypeConverter<DateTime, String> {     public string Convert(DateTime source)     {         return source.ToString("mm - yyyy", CultureInfo.InvariantCulture);     } } 

However, I cannot find any overload of the Mapper.Map<>() method to specify a profile. I also had a look at the Configuration object with no luck.
The last registered profile always takes precedence.

Is there a way to use profiles for this purpose?

like image 666
sebd Avatar asked Feb 02 '10 10:02

sebd


People also ask

Does AutoMapper map both ways?

Yes, or you can call CreateMap<ModelClass, ViewModelClass>(). ReverseMap() .

When should you not use AutoMapper?

If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.

Is AutoMapper faster than manual mapping?

Inside this article, it discusses performance and it indicates that Automapper is 7 times slower than manual mapping.

What is an AutoMapper profile?

automapper Profiles Basic Profile Profiles permit the programmer to organize maps into classes, enhancing code readability and maintainability. Any number of profiles can be created, and added to one or more configurations as needed. Profiles can be used with both the static and instance-based APIs.


1 Answers

Profiles are for segregating common configuration applied across several type maps, like formatting. However, type maps are still global. You're better off creating separate Configuration objects, and creating a separate MappingEngine for each. The Mapper class is merely a static facade over each of those, with some lifecycle management.

like image 182
Jimmy Bogard Avatar answered Sep 19 '22 13:09

Jimmy Bogard