What's the alternative to IValueFormatter in AutoMapper?

我只是一个虾纸丫 提交于 2019-12-11 02:54:01

问题


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>());


来源:https://stackoverflow.com/questions/30137734/whats-the-alternative-to-ivalueformatter-in-automapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!