AutoMapper issue

后端 未结 3 413
臣服心动
臣服心动 2020-12-18 07:48

Trying to automap some objects.
Source objects has properties with _ before name, destination objects - have not. Is it possible to implement ONE map creation, that auto

相关标签:
3条回答
  • 2020-12-18 08:01

    Something I added recently to AutoMapper might help you - custom naming conventions. If you check out the trunk (R107), look around for INamingConvention. Right now, I have two naming conventions (PascalCase and lower_case_underscore), but it's really just a matter of figuring out the right RegEx to get you going:

    INamingConvention.cs

    Right now, naming conventions are global and profile-scoped. Since this feature is new, there isn't any documentation other than the tests.

    0 讨论(0)
  • 2020-12-18 08:08

    This is how I'm doing it

    Mapper.Initialize(cfg =>
            {
                cfg.RecognizeDestinationPrefixes(new []{"_"});
                cfg.RecognizePrefixes(new[] { "_" });
    
                cfg.CreateMap<To, From>().ReverseMap();
            });
    
    0 讨论(0)
  • 2020-12-18 08:15

    For this you could add a custom mapping to solve this particular case:

    Mapper.CreateMap<From, To>()
       .ForMember( dest => dest.Id, opt => opt.MapFrom( src => src._Id ) )
       .ForMember( dest => dest.Name, opt => opt.MapFrom( src => src._Name ) );
    
    0 讨论(0)
提交回复
热议问题