亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法正確使用自動映射器來實現具有鑒別器的繼承?

有沒有辦法正確使用自動映射器來實現具有鑒別器的繼承?

C#
慕萊塢森 2022-08-20 17:27:30
我的業務邏輯 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    {    }}
查看完整描述

1 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

這是我的完整解決方案,它涵蓋了所有映射組合


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

                    .Include<Pet, Dto.Dog>()

                    .Include<Pet, Dto.Cat>()

                    .Include<Pet, Dto.Mouse>()


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

                    .ForMember(dst => dst.Description, opt => opt.Ignore())


                    .ConstructUsing((src, context) =>

                    {

                        switch (src.Type)

                        {

                            case 1: return context.Mapper.Map(src, new Dto.Dog { }, context);

                            case 2: return context.Mapper.Map(src, new Dto.Cat { }, context);

                            case 3: return context.Mapper.Map(src, new Dto.Mouse { }, context);

                            default: return context.Mapper.Map(src, new Dto.Pet { }, context);

                        }

                    })

                ;


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

                    .ForMember(dst => dst.Description, opt => opt.MapFrom(src => "This is a dog"))

                ;


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

                    .ForMember(dst => dst.Description, opt => opt.MapFrom(src => "This is a cat"))

                ;


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

                    .ForMember(dst => dst.Description, opt => opt.MapFrom(src => "This is a mouse"))

                ;


            }).CreateMapper();


            // Test

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

            };


            // Full mixed collection

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


            // Single item

            var dog = mapper.Map<Pet, Dto.Pet>(pets[0]); 

            var dog2 = mapper.Map<Pet, Dto.Dog>(pets[0]); 

        }

    }


    public class Pet

    {

        public string PetName;

        public int Type;

    }

}


namespace Dto

{

    public class Pet

    {

        public string Name;

        public string Description;

    }


    public class Dog : Pet

    {

    }


    public class Cat : Pet

    {

    }


    public class Mouse : Pet

    {

    }

}


查看完整回答
反對 回復 2022-08-20
  • 1 回答
  • 0 關注
  • 93 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號