AutoMapper-继承保留参考 [英] AutoMapper - Inheritance preserve reference

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

问题描述

我有以下情况

实体框架类类:

public class Block
{
    public Guid Id { get; set; }
    public ICollection<BlockLocation> BlockLocations { get; set; }
    public BlockType Type { get; set; }
}

public class BlockLocation
{
    public Guid Id { get; set; }
    public Guid BlockId { get; set; }
    public Block Block { get; set; }
}

我的域实体看起来像

public class Block
{
    public Block(BlockType type = BlockType.None) : this()
    {
        Type = type;
    }

    private Block() { }

    public Guid Id { get; set; }
    public List<BlockLocation> BlockLocations { get; set; }
    public BlockType Type { get; set; }
}

public class LiveBlock : Block
{
    public LiveBlock() : base(BlockType.Live) { }
}

public class UnsequencedBlock : Block
{
    public UnsequencedBlock() : base(BlockType.Unsequenced) { }
}

public class BlockLocation
{
    public Guid Id { get; set; }
    public Guid BlockId { get; set; }
    public Block Block { get; set; }
}

public enum BlockType
{
    None = 0,
    Live,
    Unsequenced
}

我想做的是从Entity Framework映射到Domain实体到子类型,并保留引用,以免堆栈溢出

And what I want to do is map from Entity Framework to a Domain entity to the child type and also preserve the reference so that I don't get a stack overflow

我的映射是

cfg.CreateMap<Data.Block, Domain.LiveBlock>();
cfg.CreateMap<Data.Block, Domain.UnsequencedBlock>();
cfg.CreateMap<Data.Block, Domain.Block>().PreserveReferences().ConstructUsing((block, context) =>
        {
            if (block.Type == BlockType.Live)
            {
                // This loops until stack overflow
                return context.Mapper.Map<Domain.LiveBlock>(block);
            }

            if (block.Type == BlockType.Unsequenced)
            {
                return context.Mapper.Map<Domain.LiveBlock>(block);
            }

            return context.Mapper.Map<Domain.Block>(block);
        });

 cfg.CreateMap<Data.BlockLocation, Domain.BlockLocation>();

我正在尝试执行以下操作:

And I'm trying to do the following:

// This is the EF entity
var block = new Data.Block
{
    Id = Guid.NewGuid(),
    Type = BlockType.Live,
    BlockLocations = new List<Data.BlockLocation>
    {
        new BlockLocation {Id = Guid.NewGuid()},
        new BlockLocation {Id = Guid.NewGuid()}
    }
};

block.BlockLocations[0].Block = block;
block.BlockLocations[1].Block = block;

// Trying to create a Domain entity
var domainBlock = Mapper.Map<Data.Block, Domain.Block>(block);

我要实现的结果是 domainBlock 的类型为LiveBlock,并具有BlockLocations列表,这些BlockLocations的LiveBlock实体与其 Block 属性相同

The result that I want to achieve is for domainBlock to be of type LiveBlock and have a list of BlockLocations which in turn have the same LiveBlock entity as their Block property

我得到的是ConstructUsing中的一个循环,直到出现堆栈溢出.

What I get is a loop in ConstructUsing, until I get stack overflow.

现在,我的问题是:

  • 这可以通过AutoMapper实现吗?
  • 如果是,可以使用ContructUsing完成吗?我也尝试过ConvertUsing,但得到的结果相同.
  • 也许还有其他方法吗?

我知道一种解决方法是忽略 Domain.Block 中的 BlockLocations 属性,并分别映射它们,但是我想使用Automapper来实现自动.

I know that a way of doing to would be to Ignore the BlockLocations property from Domain.Block and map them separately, but I would like to have Automapper to that automatically.

谢谢您的帮助.

推荐答案

在Lucian的帮助下使其正常工作

Got it working with Lucian's help

我将映射器更改为以下

cfg.CreateMap<Data.Block, Domain.LiveBlock>().PreserveReferences();
cfg.CreateMap<Data.Block, Domain.UnsequencedBlock>().PreserveReferences();
cfg.CreateMap<Data.Block, Domain.Block>().PreserveReferences().ConstructUsing((block, context) =>
    {
        if (block.Type == BlockType.Live)
        {
            var b = new LiveBlock();
            return context.Mapper.Map(block, b, context);
        }

        if (block.Type == BlockType.Unsequenced)
        {
            var unsequencedBlock = new UnsequencedBlock();
            return context.Mapper.Map(block, unsequencedBlock, context);
        }

        return context.Mapper.Map<Domain.Block>(block);
    });

cfg.CreateMap<Data.BlockLocation, Domain.BlockLocation>().PreserveReferences();

secred是使用以上下文为参数的Map方法

The secred was usint the Map method that takes the context as a parameter

context.Mapper.Map(block, unsequencedBlock, context);

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

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