Globally apply value resolver with AutoMapper

后端 未结 1 398
忘了有多久
忘了有多久 2021-02-07 06:00

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

1条回答
  •  太阳男子
    2021-02-07 06:47

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

    Mapper.CreateMap().ConvertUsing();
    

    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.

    0 讨论(0)
提交回复
热议问题