AutoMapper问题 [英] AutoMapper issue

查看:120
本文介绍了AutoMapper问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图自动映射某些对象。

源对象有_性能名称前,目标对象 - 没有。
是否有可能实现一个地图的创建,这automapper都会_properties到属性映射

所有源类型。

 类MyMapper< TFrom,TTO> {
    TTO PerformMap(TFrom fromObject){
        Mapper.CreateMap&下;从,到大于(); //?
        TTO结果= Mapper.Map<从,到大于(fromObject);
        //result.Id.ShouldBe(value从TFrom._Id);
        返回结果;
    }
}从类
{
    公众诠释_id {搞定;组; }
    公共字符串_Name {搞定;组; }
}类要
{
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }
}


解决方案

我最近加入到AutoMapper东西可以帮助你 - 自定义命名约定。如果检查出中继线(R107),到处寻找INamingConvention。现在,我有两个命名约定(PascalCase和lower_case_underscore),但它实际上只是一个计算正确的正则表达式的事让你去:

INamingConvention.cs

现在,命名规范是全球性和外形范围的。因为该功能是新的,有不低于测试以外的任何文件。

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; }
}

解决方案

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.

这篇关于AutoMapper问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆