繁体   English   中英

AutoMapper不适用于Container类

[英]AutoMapper is not working for a Container class

我有一个AutoMapper问题,一直让我疯狂太久了。 类似的问题也发布在AutoMapper用户网站上,但没有太多的爱。

总结是我有一个容器类,它包含一个组件字典。 组件是公共基类的派生对象。 我也有一个并行结构,我用作我要映射的DTO对象。

生成的错误似乎表示映射器无法映射我在CreateMap调用中包含的两个类之间。 我认为错误与我有一个不包含容器层次结构的对象的Dictionary这一事实有关。

我提前为下面的代码长度道歉。 我的简单测试用例有效。 毋庸置疑,只有更复杂的情况才会失败。

以下是课程:

#region Dto objects

public class ComponentContainerDTO
{
    public Dictionary<string, ComponentDTO> Components { get; set; }

    public ComponentContainerDTO()
    {
        this.Components = new Dictionary<string, ComponentDTO>();
    }
}

public class EntityDTO : ComponentContainerDTO
{
    public int Id { get; set; }

}

public class ComponentDTO
{
    public EntityDTO Owner { get; set; }
    public int Id { get; set; }
    public string Name { get; set; }
    public string ComponentType { get; set; }
}

public class HealthDTO : ComponentDTO
{
    public decimal CurrentHealth { get; set; }

}

public class PhysicalLocationDTO : ComponentDTO
{
    public Point2D Location { get; set; }
}
#endregion


#region Domain objects

public class ComponentContainer
{
    public Dictionary<string, Component> Components { get; set; }

    public ComponentContainer()
    {
        this.Components = new Dictionary<string, Component>();
    }
}

public class Entity : ComponentContainer
{
    public int Id { get; set; }

}

public class Component
{
    public Entity Owner { get; set; }
    public int Id { get; set; }
    public string Name { get; set; }
    public string ComponentType { get; set; }

}

public class Health : Component
{
    public decimal CurrentHealth { get; set; }
}

public struct Point2D
{
    public decimal X;
    public decimal Y;

    public Point2D(decimal x, decimal y)
    {
        X = x;
        Y = y;
    }
}

public class PhysicalLocation : Component
{
    public Point2D Location { get; set; }
}
#endregion

代码:

var entity = new Entity() { Id = 1 };
var healthComponent = new Health() { CurrentHealth = 100, Owner = entity, Name = "Health", Id = 2 };
entity.Components.Add("1", healthComponent);
var locationComponent = new PhysicalLocation() { Location = new Point2D() { X = 1, Y = 2 }, Owner = entity, Name = "PhysicalLocation", Id = 3 };
entity.Components.Add("2", locationComponent);

Mapper.CreateMap<ComponentContainer, ComponentContainerDTO>()
    .Include<Entity, EntityDTO>();

Mapper.CreateMap<Entity, EntityDTO>();

Mapper.CreateMap<Component, ComponentDTO>()
    .Include<Health, HealthDTO>()
    .Include<PhysicalLocation, PhysicalLocationDTO>();

Mapper.CreateMap<Component, ComponentDTO>();
Mapper.CreateMap<Health, HealthDTO>();
Mapper.CreateMap<PhysicalLocation, PhysicalLocationDTO>();

Mapper.AssertConfigurationIsValid();

var targetEntity = Mapper.Map<Entity, EntityDTO>(entity);

我调用Map()时的错误(缩写堆栈爬行):

AutoMapper.AutoMapperMappingException was unhandled
  Message=Trying to map MapperTest1.Entity to MapperTest1.EntityDTO.
Using mapping configuration for MapperTest1.Entity to MapperTest1.EntityDTO
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
  Source=AutoMapper
  StackTrace:
       at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
.
.
.

  InnerException: AutoMapper.AutoMapperMappingException
       Message=Trying to map System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[MapperTest1.Component, ElasticTest1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[MapperTest1.ComponentDTO, ElasticTest1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].
Using mapping configuration for MapperTest1.Entity to MapperTest1.EntityDTO
Destination property: Components
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
       Source=AutoMapper
       StackTrace:
            at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap)
.
.

       InnerException: AutoMapper.AutoMapperMappingException
            Message=Trying to map System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[MapperTest1.Component, ElasticTest1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[MapperTest1.ComponentDTO, ElasticTest1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].
Using mapping configuration for MapperTest1.Entity to MapperTest1.EntityDTO
Destination property: Components
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
            Source=AutoMapper
            StackTrace:
                 at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
                 .
            InnerException: AutoMapper.AutoMapperMappingException
                 Message=Trying to map MapperTest1.Component to MapperTest1.ComponentDTO.
Using mapping configuration for MapperTest1.Health to MapperTest1.HealthDTO
Destination property: Components
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
                 Source=AutoMapper
                 StackTrace:
                      at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
                      .
.

                 InnerException: AutoMapper.AutoMapperMappingException
                      Message=Trying to map System.Decimal to System.Decimal.
Using mapping configuration for MapperTest1.Health to MapperTest1.HealthDTO
Destination property: CurrentHealth
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
                      Source=AutoMapper
                      StackTrace:
                           at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap)

.
.
                      InnerException: System.InvalidCastException
                           Message=Unable to cast object of type 'MapperTest1.ComponentDTO' to type 'MapperTest1.HealthDTO'.
                           Source=Anonymously Hosted DynamicMethods Assembly
                           StackTrace:
                                at SetCurrentHealth(Object , Object )
.
.

先感谢您。

干草堆

这是一个已在1.1.0.184版本中修复的错误。

谢谢吉米修理它。

干草堆

暂无
暂无

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

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