Conditionally mapping one source type to two destination types

后端 未结 4 812
遇见更好的自我
遇见更好的自我 2020-12-20 01:15

I have a source DTO like this

public class Member 
{
    public string MemberId {get;set;}
    public string MemberType {get;set;}
    public string Name {g         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-20 01:46

    For version 5 and above you could try below code:

    using System;
    using AutoMapper;
    
    namespace AutoMapOneToMulti
    {
        class Program
        {
            static void Main(string[] args)
            {
                RegisterMaps();
    
                var s = new Source { X = 1, Y = 2 };
    
                Console.WriteLine(s);
                Console.WriteLine(Mapper.Map(s));
                Console.WriteLine(Mapper.Map(s));
    
                Console.ReadLine();
            }
            static void RegisterMaps()
            {
                Mapper.Initialize(cfg => cfg.AddProfile());
            }
        }
        public class GeneralProfile : Profile
        {
            public GeneralProfile()
            {
                CreateMap();
                CreateMap();
            }
        }
        public class Source
        {
            public int X { get; set; }
    
            public int Y { get; set; }
    
            public override string ToString()
            {
                return string.Format("Source = X : {0}, Y : {1}", X, Y);
            }
        }
        public class Destination1
        {
            public int X { get; set; }
    
            public int Y { get; set; }
            public override string ToString()
            {
                return string.Format("Destination1 = X : {0}, Y : {1}", X, Y);
            }
        }
        public class Destination2
        {
            public int X { get; set; }
    
            public int Y { get; set; }
            public override string ToString()
            {
                return string.Format("Destination2 = X : {0}, Y : {1}", X, Y);
            }
        }
    }
    

    And for version below 5 you could try this:

    using System;
    using AutoMapper;
    
    namespace AutoMapOneToMulti
    {
        class Program
        {
            static void Main(string[] args)
            {
                RegisterMaps();
    
                var s = new Source { X = 1, Y = 2 };
    
                Console.WriteLine(s);
                Console.WriteLine(Mapper.Map(s));
                Console.WriteLine(Mapper.Map(s));
    
                Console.ReadLine();
            }
            static void RegisterMaps()
            {
                Mapper.Initialize(cfg => cfg.AddProfile());
            }
        }
        public class GeneralProfile : Profile
        {
            protected override void Configure()
            {
                CreateMap();
                CreateMap();
            }
        }
        public class Source
        {
            public int X { get; set; }
    
            public int Y { get; set; }
    
            public override string ToString()
            {
                return string.Format("Source = X : {0}, Y : {1}", X, Y);
            }
        }
        public class Destination1
        {
            public int X { get; set; }
    
            public int Y { get; set; }
            public override string ToString()
            {
                return string.Format("Destination1 = X : {0}, Y : {1}", X, Y);
            }
        }
        public class Destination2
        {
            public int X { get; set; }
    
            public int Y { get; set; }
            public override string ToString()
            {
                return string.Format("Destination2 = X : {0}, Y : {1}", X, Y);
            }
        }
    }
    

    If you want a dynamic function, use this extension:

    public static dynamic DaynamicMap(this Source source)
    {
        if (source.X == 1)
            return Mapper.Map(source);
        return Mapper.Map(source);
    }
    
    Console.WriteLine(new Source { X = 1, Y = 2 }.DaynamicMap());
    Console.WriteLine(new Source { X = 2, Y = 2 }.DaynamicMap());
    

提交回复
热议问题