简体   繁体   English

发出映射器奉承和属性名称不匹配

[英]Emit Mapper Flattering and property name mismatch

How to map User class to UserModel class using Emit Mapper? 如何使用Emit Mapper将User类映射到UserModel类?

    public class User
    {
        public Guid Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public IList<Role> Roles { get; set; }

        public Company Company { get; set; }        
    }

    public class UserModel
    {
        public Guid Id { get; set; }

        public Guid CompanyId { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }      

        public IList<RoleModel> Roles { get; set; }
}

There several problems: 有几个问题:

  • I need to flatten the object such that I will have CompanyId instead of the Company object. 我需要展平对象,以使我将拥有CompanyId而不是Company对象。
  • Company object has property Id, in the UserModel I have CompanyId which corresponds to the company id, but property names do not match. 公司对象具有属性ID,在UserModel中,我具有与公司ID对应的CompanyId,但是属性名称不匹配。
  • I need to map List<Role> to List<RoleModel> 我需要将List<Role>映射到List<RoleModel>

To get flattened model you could check this example . 要弄平模型,可以检查此示例 But it seems that by default it has a convention of having sub class property name as a prefix in the target. 但似乎默认情况下,它有一个约定,即以子类属性名称作为目标中的前缀。

Source 资源

public class SourceObject
{
public SourceSubObject SomeClass { get; set; }
}

public SourceSubObject
{
    public int Age { get; set; }
}

Target 目标

public class Target
{
public int SomeClassAge  { get; set; }
}

Second, one options is to let the default settings copy those properties that it can copy and manually do the rest 其次,一种选择是让默认设置复制那些可以复制的属性,然后手动执行其余操作

var target = ObjectMapperManager.DefaultInstance.GetMapper<Source, Target>().Map(source);
target.CompanyId = target.Company.CompanyId;

Or if you need to reuse the mapping the create custom mapper 或者,如果您需要重用映射,请创建自定义映射器

Custom mapper 自定义映射器

private Target Converter(Source source)
{
   var target = new Target();
   target.CompanyId = source.Company.CompanyId;
   return target;
}

Usage 用法

var mapper = new DefaultMapConfig().ConvertUsing<Source, Target>(Converter);
var target = ObjectMapperManager.DefaultInstance.GetMapper<Source, Target>(mapper).Map(source);

Update 更新

What comes to the Role & RoleModel mapping. 什么是角色和角色模型映射。 It seems that in this case you need to have Deep copy enabled and depending on the class(es) definitions you can either copy it directly or do some custom mapping. 在这种情况下,您似乎需要启用深度复制,并且可以根据类定义直接复制它或进行一些自定义映射。

ObjectMapperManager.DefaultInstance.GetMapper<Source, Target>(new DefaultMapConfig().DeepMap<ClassToDeepMap>().DeepMap<ClassToDeepMap>()).Map(source, target);
  • For flattering I was using configuration from the samples in Emit Mapper source files: http://emitmapper.codeplex.com/SourceControl/changeset/view/69894#1192663 为了讨好我,我使用了Emit Mapper源文件中示例中的配置: http : //emitmapper.codeplex.com/SourceControl/changeset/view/69894#1192663

  • To make the names to match in Company class should be the field with the name Id 要使名称在Company类中匹配,应该是名称为Id的字段

  • For mapping List<Role> to List<RoleModel> I was using custom converter: 为了将List<Role>映射到List<RoleModel>我使用了自定义转换器:

     public class EntityListToModelListConverter<TEntity, TModel> { public List<TModel> Convert(IList<TEntity> from, object state) { if (from == null) return null; var models = new List<TModel>(); var mapper = ObjectMapperManager.DefaultInstance.GetMapper<TEntity, TModel>(); for (int i = 0; i < from.Count(); i++) { models.Add(mapper.Map(from.ElementAt(i))); } return models; } } 

    So all together: 所以一起:

      var userMapper = ObjectMapperManager.DefaultInstance.GetMapper<User, UserModel>( new FlatteringConfig().ConvertGeneric(typeof(IList<>), typeof(IList<>), new DefaultCustomConverterProvider(typeof(EntityListToModelListConverter<,>)))); 
  • There is a problem, using Flatterning Configuration with Custom converters, check my question: Emit Mapper Flattering with Custom Converters 使用自定义转换器使用Flatterning Configuration时出现问题,请检查我的问题:使用自定义转换器发射Mapter 奉承

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM