Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globally apply value resolver with AutoMapper

Tags:

automapper

I'm trying to have AutoMapper take care of localizing all DateTime properties on our view models for us. We use UTC everywhere in our system and store everything in UTC in the database, but we'd like to automatically convert that to a user's time zone for display.

After looking at all the options, I settled on using a ValueResolver. Here's the gist of the resolver:

public class LocalizedDateTimeFormatter : ValueResolver<DateTime, DateTime>
{
    protected override DateTime ResolveCore(DateTime source)
    {
        // get company

        return company.TimeZone.ConvertFromUtc(source);
    }
}

I'm setting up the mapping like so:

Mapper.CreateMap<Entity, Model>()
    .ForMember(dest => dest.Foo, opt => opt.ResolveUsing<LocalizedDateTimeFormatter>()
                                            .FromMember(src => src.Foo));

This all works fine, and I'm happy with it. However, ideally we'd like a convention of all DateTime properties on a view model to use this resolver by default. I started down the path of reflecting over the view model properties, picking out DateTime ones, and using the overloads of ForMember and FromMember that take in property string names, but that seemed... ugly. Plus duplicating AutoMapper's nested property name building logic would break down pretty quick.

Question: Is there any easy way to tell AutoMapper to globally use a ValueResolver like this? To say "any time you're mapping a DateTime property on a source to a DateTime property on a destination, use this resolver"?

I looked through AutoMapper's tests and didn't see anything that'd work.

Thanks!

like image 592
Darrell Mozingo Avatar asked Apr 14 '11 16:04

Darrell Mozingo


People also ask

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.

Does AutoMapper use reflection?

When you call CreateMap, AutoMapper uses optimizers to build the code for getting/setting values on source/destination types. Currently, it uses a combination of Reflection.

Where is AutoMapper used?

AutoMapper is used whenever there are many data properties for objects, and we need to map them between the object of source class to the object of destination class, Along with the knowledge of data structure and algorithms, a developer is required to have excellent development skills as well.

What is AutoMapper in C#?

AutoMapper is a simple C# library that transforms one object type to another object type, which means, it's a mapper between two objects. AutoMapper is the convention-based object to object mapper.


1 Answers

Yes - but with a slight change in ordering of the MapperRegistry. First, create a type converter from DateTime to DateTime:

Mapper.CreateMap<DateTime, DateTime>().ConvertUsing<CompanyTimeConverter>();

Your CompanyTimeConverter code looks pretty much like the value resolver you had, except it inherits from TypeConverter.

Next, you have to change the order of the MapperRegistry (I'm going to change this going forward, it makes more sense):

MapperRegistry.AllMappers = () => new IObjectMapper[] {
    new DataReaderMapper(),
    new TypeMapMapper(TypeMapObjectMapperRegistry.AllMappers()),
    new StringMapper(),
    new FlagsEnumMapper(),
    new EnumMapper(),
    new ArrayMapper(),
    new EnumerableToDictionaryMapper(),
    new DictionaryMapper(),
    new ListSourceMapper(),
    new CollectionMapper(),
    new EnumerableMapper(),
    new TypeConverterMapper(),
    new AssignableMapper(),
    new NullableMapper()
};

Originally, the "Assignable" mapper came before the "TypeConverter" mapper, so that if two types were assignable to each other, it would just do that.

like image 125
Jimmy Bogard Avatar answered Oct 20 '22 03:10

Jimmy Bogard