自动映射器问题

发布于 2024-07-16 21:48:47 字数 569 浏览 1 评论 0原文

尝试自动映射一些对象。
源对象具有名称前带有 _ 的属性,而目标对象则没有。 是否可以实现一个映射创建,即自动映射器将所有 _properties 映射到属性
对于所有源类型。

class MyMapper<TFrom, TTo>{
    TTo PerformMap(TFrom fromObject){
        Mapper.CreateMap<From, To>(); // ???
        TTo result = Mapper.Map<From, To>(fromObject);
        //result.Id.ShouldBe(value from TFrom._Id);
        return result;
    }
}

class From
{
    public int _Id { get; set; }
    public string _Name { get; set; }
}

class To
{
    public int Id { get; set; }
    public string Name { get; set; }
}

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 automapper would map all _properties to properties
for all source types.

class MyMapper<TFrom, TTo>{
    TTo PerformMap(TFrom fromObject){
        Mapper.CreateMap<From, To>(); // ???
        TTo result = Mapper.Map<From, To>(fromObject);
        //result.Id.ShouldBe(value from TFrom._Id);
        return result;
    }
}

class From
{
    public int _Id { get; set; }
    public string _Name { get; set; }
}

class To
{
    public int Id { get; set; }
    public string Name { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染年凉城似染瑾 2024-07-23 21:48:47

我最近添加到 AutoMapper 中的一些内容可能会对您有所帮助 - 自定义命名约定。 如果您检查主干(R107),请四处寻找 INamingConvention。 现在,我有两个命名约定(PascalCase 和 lower_case_underscore),但这实际上只是找出正确的正则表达式来让您继续前进的问题:

INamingConvention.cs

目前,命名约定是全局的且仅限于配置文件范围。 由于此功能是新功能,因此除了测试之外没有任何文档。

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.

山人契 2024-07-23 21:48:47

我就是这样做的

Mapper.Initialize(cfg =>
        {
            cfg.RecognizeDestinationPrefixes(new []{"_"});
            cfg.RecognizePrefixes(new[] { "_" });

            cfg.CreateMap<To, From>().ReverseMap();
        });

This is how I'm doing it

Mapper.Initialize(cfg =>
        {
            cfg.RecognizeDestinationPrefixes(new []{"_"});
            cfg.RecognizePrefixes(new[] { "_" });

            cfg.CreateMap<To, From>().ReverseMap();
        });
那小子欠揍 2024-07-23 21:48:47

为此,您可以添加自定义映射来解决此特殊情况:

Mapper.CreateMap<From, To>()
   .ForMember( dest => dest.Id, opt => opt.MapFrom( src => src._Id ) )
   .ForMember( dest => dest.Name, opt => opt.MapFrom( src => src._Name ) );

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 ) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文