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!