AutoMapper 시리즈 1 - 간단 한 사용

7169 단어 C#
공식 문서 주소:http://automapper.readthedocs.io/en/latest/Getting-started.html
포함: 일반적인 맵, 맵 설정 (지정 대상, 대상 무시), 집합 대상, 계승 처리
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//
using AutoMapper;
using AutoMapper.Mappers;

namespace LibStudy
{
    class Program
    {
        static void Main(string[] args)
        {
            #region demo1
            //var order = new Order
            //{
            //    Customer = new Customer
            //    {
            //        Name = "Jon"
            //    }
            //};
            //order.AddOrderLineItem(new Product
            //{
            //    Name = "Bosco",
            //    Price = 4.99m
            //}, 15);

            //Mapper.Initialize(x => x.CreateMap());

            //OrderDto dto = Mapper.Map(order);
            //Console.WriteLine("CustomerName:" + dto.CustomerName);
            //Console.WriteLine("Total:" + dto.Total);
            #endregion

            #region demo2
            //var c = new CalendarEvent
            //{
            //    EventDate = new DateTime(2018, 01, 18, 08, 08, 08),
            //    Title = "Company Oping Date"
            //};

            //Mapper.Initialize(x => x.CreateMap()
            //                        .ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.EventDate.Date))
            //                        .ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.EventDate.Hour))
            //                        .ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.EventDate.Minute))
            //                 );

            //CalendarEventForm target = Mapper.Map(c);

            //Console.WriteLine("EventDate:" + target.EventDate);
            //Console.WriteLine("EventHour:" + target.EventHour);
            //Console.WriteLine("EventMinute:" + target.EventMinute);
            //Console.WriteLine("Title:" + target.Title);
            #endregion

            #region demo3
            //Mapper.Initialize(x => x.CreateMap()
            //                        //.ForMember(d => d.item2, o => o.MapFrom(src => src.item1))
            //                        .ForMember(d => d.item2, o => o.Ignore()) //           
            //);

            //var s = new source { item1 = "Jon" };
            //var t = Mapper.Map(s);
            //if (t != null && !string.IsNullOrWhiteSpace(t.item2))
            //    Console.WriteLine(t.item2);
            //else
            //    Console.WriteLine("map error");
            #endregion

            #region list or array
            //var list = new source[] {
            //    new source {item1 = "J"},
            //    new source {item1 = "O"},
            //    new source {item1 = "N"}
            //};
            //Mapper.Initialize(x => x.CreateMap()
            //                        .ForMember(d => d.item2, o => o.MapFrom(src => src.item1))
            //);

            //IEnumerable tinymaps = Mapper.Map>(list); //          ,     
            ////IEnumerable ienumerableDest = Mapper.Map>(list);
            ////ICollection icollectionDest = Mapper.Map>(list);
            ////IList ilistDest = Mapper.Map>(list);
            ////List listDest = Mapper.Map>(list);
            //tinymaps.All(x => { Console.WriteLine(x.item2); return true; });
            #endregion

            #region inherit
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap()
                   .Include(); //  ,child parent      
                cfg.CreateMap().ForMember(d => d.parentname, o => o.MapFrom(src => src.childname));
            });
            var sources = new parent[]
            {
                new parent() { parentname="vs"},
                new child() { childname="vscode"},
                new parent() { parentname="c#"}
            };

            //       
            var destinations = Mapper.Map(sources);
            destinations.All(x => { Console.WriteLine(x.parentname); return true; });
            #endregion

            Console.ReadKey();
        }
    }

    #region demo4
    public class child : parent
    {
        public string childname { get; set; }
    }

    public class parent
    {
        public string parentname { get; set; }
    }

    public class toparent
    {
        public string parentname { get; set; }
    }

    #endregion

    #region demo3
    public class source
    {
        public string item1 { get; set; }
    }

    public class tosource
    {
        public string item2 { get; set; }
    }
    #endregion

    #region demo2
    public class CalendarEvent
    {
        public DateTime EventDate { get; set; }
        public string Title { get; set; }
    }

    public class CalendarEventForm
    {
        public DateTime EventDate { get; set; }
        public int EventHour { get; set; }
        public int EventMinute { get; set; }
        public string Title { get; set; }
    }
    #endregion

    #region demo1
    public class Order
    {
        private readonly IList _orderLineItems = new List();
        public Customer Customer { get; set; }
        public OrderLineItem[] GetOrderLineItems()
        {
            return _orderLineItems.ToArray();
        }
        public void AddOrderLineItem(Product product, int quantity)
        {
            _orderLineItems.Add(new OrderLineItem(product, quantity));
        }
        public decimal GetTotal()
        {
            return _orderLineItems.Sum(li => li.GetTotal());
        }
    }

    public class OrderLineItem
    {
        public OrderLineItem(Product product, int quantity)
        {
            Product = product;
            Quantity = quantity;
        }
        public Product Product { get; private set; }
        public int Quantity { get; private set; }
        public decimal GetTotal()
        {
            return Quantity * Product.Price;
        }
    }

    public class Product
    {
        public decimal Price { get; set; }
        public string Name { get; set; }
    }

    public class Customer
    {
        public string Name { get; set; }
    }

    public class OrderDto
    {
        public string CustomerName { get; set; }
        public decimal Total { get; set; }
    }
    #endregion
}

좋은 웹페이지 즐겨찾기