What's the alternative to IValueFormatter in A

2019-07-20 21:17发布

问题:

So I started using AutoMapper for my ASP.NET MVC project, looked at a couple of examples and wrote my first IValueFormatter implementation when a blue wiggly popped up for IValueFormatter: Interface 'AutoMapper.IValueFormatter' is obsolete: "Formatters should not not be used". Well, okay, then how am I supposed to e.g. convert a DateTime to, say, a string?

回答1:

Just create mapping for the property without using IValueFormatter. For example:

Mapper.CreateMap<DbItem, ItemView>()                  
    .ForMember(item => item.DateCreated, opt => opt.ResolveUsing(i => {
        var date = i.DateCreated;
        return date.ToShortDateString();                      
    }));

Instead of :

Mapper.CreateMap<DbItem, ItemView>().ForMember(item => item.DateCreated, 
item => item.AddFormatter<ItemDateCreatedFormater>());