AutoMapper - Inheritance preserve reference

2019-08-24 04:26发布

问题:

I have the following scenario

Entity framework classes classes:

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

And my Domain Entities look like

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
}

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

My mappings are

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

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

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

Now, my questions are:

  • Can this be achieved with AutoMapper?
  • If yes, can it be done with ContructUsing? I've also tried ConvertUsing, but I get the same result.
  • Some other approach maybe?

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.

Thank you for your help.

回答1:

Got it working with Lucian's help

I changed the mapper to the following

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();

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

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