自动映射器和基本类型

自动映射器和基本类型

本文介绍了自动映射器和基本类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EntityDtos集合。

I have a collection of EntityDtos.

每个EntityDto都有一个称为EntityType的属性。

Each EntityDto has a property called EntityType.

这些EntityType的每个都对应于一个不同的子类,类似这样的

Each of these EntityTypes correspond to a different subclass, something like this

abstract class EntityBase { EntityType = EntityType.Base; }
class EntityOne : EntityBase { EntityType = EntityType.One; }
class EntityTwo : EntityBase { EntityType = EntityType.Two; }

我正在尝试映射到EntityBase的集合。 AutoMapper失败,并显示错误无法创建抽象类的实例。我有Type枚举,因此知道每个类型都应映射到什么类型...但是,实际上,只希望它们都映射到我的EntityBase集合中。

I am trying to map to a collection of EntityBase. AutoMapper fails with the error "Instances of abstract classes cannot be created". I have the Type enum, and thus know what type each should be mapped to...but really, just want them all mapped into my EntityBase collection.

我可以t弄清楚这一点...

I can't figure this out...

我有这个工作,但是非常难看。

I have this working, but it's VERY ugly.

Mapper.CreateMap<EntityCollectionDto, EntityCollection>().ForMember(
    s => s.Entities, d => d.MapFrom(
        x => new List<EntityBase>(
            from p in x.Entitys
            select p.EntityType == EntityType.One ? Mapper.Map<EntityOne>(p) as EntityBase
                : p.EntityType == EntityType.Two ? Mapper.Map<EntityTwo>(p) as EntityBase
                : Mapper.Map<EntityThree>(p) as EntityBase
            )
        )
    );

 Mapper.CreateMap<EntityDto, EntityOne>();
 Mapper.CreateMap<EntityDto, EntityTwo>();


推荐答案

我不知道您是否会喜欢这样更好,但假设实体类别如下:

I don't know if you'll like this any better, but assuming the entity classes as follows:

public abstract class EntityBase
{
    public EntityType EntityType { get { return EntityType.Base; } }
}
public class EntityOne : EntityBase
{
    public new EntityType EntityType { get { return EntityType.One; } }
}
public class EntityTwo : EntityBase
{
    public new EntityType EntityType { get { return EntityType.Two; } }
}
public class EntityThree : EntityBase
{
    public new EntityType EntityType { get { return EntityType.Three; } }
}
public class EntityCollection
{
    public IList<EntityBase> Entities { get; set; }
}

public class EntityDto
{
    public EntityType EntityType { get; set; }
}
public class EntityCollectionDto
{
    public IList<EntityDto> Entities { get; set; }
}

您可以创建 TypeConverter

public class EntityTypeConverter : AutoMapper.TypeConverter<EntityDto, EntityBase>
{
    protected override EntityBase ConvertCore(EntityDto source)
    {
        switch (source.EntityType)
        {
            case EntityType.One:
                return AutoMapper.Mapper.Map<EntityOne>(source);
            case EntityType.Two:
                return AutoMapper.Mapper.Map<EntityTwo>(source);
            default:
                return AutoMapper.Mapper.Map<EntityThree>(source);
        }
    }
}

这将简化您的映射到:

AutoMapper.Mapper.CreateMap<EntityDto, EntityBase>()
    .ConvertUsing(new EntityTypeConverter());

AutoMapper.Mapper.CreateMap<EntityDto, EntityOne>();
AutoMapper.Mapper.CreateMap<EntityDto, EntityTwo>();
AutoMapper.Mapper.CreateMap<EntityDto, EntityThree>();

AutoMapper.Mapper.CreateMap<EntityCollectionDto, EntityCollection>();

AutoMapper.Mapper.AssertConfigurationIsValid();

因此,您仍然在 TypeConverter (我不确定是否有办法避免这种情况),但我认为最终结果会更清洁一些。

So you still have the specific mapping in the TypeConverter (I'm not sure there's a way to avoid that), but I think the end result is a bit cleaner.

这篇关于自动映射器和基本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:16