有没有办法正确使用自动映射器来实现具有鉴别器的继承?

我的业务逻辑 Pet 类中有一个 Model 类。


在这个类中,我有一个名为Type的鉴别器属性(int = 1, 2, 3, ...)


最终映射必须是特定派生类的 Dto。


我使用 ConstructUsing,但它在 Stack Overflow Exception 上继续,因为它在基类型映射规则上有一个递归。


派生的 Dto 类已正确映射,因为它们没有递归。


还尝试了 PreserveReferences() 没有运气


using AutoMapper;

using System;

using System.Collections.Generic;


namespace ConsoleAppMapper

{

    class Program

    {

        static void Main(string[] args)

        {

            var mapper = new MapperConfiguration(cfg =>

            {

                cfg.CreateMap<Pet, Dto.Pet>()

                    .PreserveReferences()

                    .ForMember(dst => dst.Name, opt => opt.MapFrom(src => src.PetName))

                    .ConstructUsing((src, context) =>

                    {

                        switch (src.Type)

                        {

                            case 1: return context.Mapper.Map<Pet, Dto.Dog>(src);

                            case 2: return context.Mapper.Map<Pet, Dto.Cat>(src);

                            case 3: return context.Mapper.Map<Pet, Dto.Mouse>(src);

                            default: return context.Mapper.Map<Pet, Dto.Pet>(src);

                        }

                    })

                ;

                cfg.CreateMap<Pet, Dto.Dog>();

                cfg.CreateMap<Pet, Dto.Cat>();

                cfg.CreateMap<Pet, Dto.Mouse>();

            }).CreateMapper();


            var pets = new List<Pet>

            {

                new Pet { PetName = "Bob", Type = 1 },

                new Pet { PetName = "Tom", Type = 2 },

                new Pet { PetName = "Jerry", Type = 3 },

                new Pet { PetName = "Duffy", Type = 4 },

            };

            var dtoList = mapper.Map<IEnumerable<Pet>, IEnumerable<Dto.Pet>>(pets);

        }

    }


    public class Pet

    {

        public string PetName;

        public int Type;

    }

}


namespace Dto

{

    public class Pet

    {

        public string Name;

    }


    public class Dog : Pet

    {

    }


    public class Cat : Pet

    {

    }


    public class Mouse : Pet

    {

    }

}



慕莱坞森
浏览 72回答 1
1回答

PIPIONE

这是我的完整解决方案,它涵盖了所有映射组合using AutoMapper;using System;using System.Collections.Generic;namespace ConsoleAppMapper{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var mapper = new MapperConfiguration(cfg =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cfg.CreateMap<Pet, Dto.Pet>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Include<Pet, Dto.Dog>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Include<Pet, Dto.Cat>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Include<Pet, Dto.Mouse>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ForMember(dst => dst.Name, opt => opt.MapFrom(src => src.PetName))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ForMember(dst => dst.Description, opt => opt.Ignore())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ConstructUsing((src, context) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (src.Type)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 1: return context.Mapper.Map(src, new Dto.Dog { }, context);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 2: return context.Mapper.Map(src, new Dto.Cat { }, context);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 3: return context.Mapper.Map(src, new Dto.Mouse { }, context);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default: return context.Mapper.Map(src, new Dto.Pet { }, context);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cfg.CreateMap<Pet, Dto.Dog>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ForMember(dst => dst.Description, opt => opt.MapFrom(src => "This is a dog"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cfg.CreateMap<Pet, Dto.Cat>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ForMember(dst => dst.Description, opt => opt.MapFrom(src => "This is a cat"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cfg.CreateMap<Pet, Dto.Mouse>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ForMember(dst => dst.Description, opt => opt.MapFrom(src => "This is a mouse"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).CreateMapper();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var pets = new List<Pet>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Pet { PetName = "Bob", Type = 1 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Pet { PetName = "Tom", Type = 2 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Pet { PetName = "Jerry", Type = 3 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Pet { PetName = "Duffy", Type = 4 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Full mixed collection&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dtoList = mapper.Map<IEnumerable<Pet>, IEnumerable<Dto.Pet>>(pets);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Single item&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dog = mapper.Map<Pet, Dto.Pet>(pets[0]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dog2 = mapper.Map<Pet, Dto.Dog>(pets[0]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Pet&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string PetName;&nbsp; &nbsp; &nbsp; &nbsp; public int Type;&nbsp; &nbsp; }}namespace Dto{&nbsp; &nbsp; public class Pet&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Name;&nbsp; &nbsp; &nbsp; &nbsp; public string Description;&nbsp; &nbsp; }&nbsp; &nbsp; public class Dog : Pet&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public class Cat : Pet&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public class Mouse : Pet&nbsp; &nbsp; {&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP