How to map Objects with Constructor parameters using Automapper

To map Objects that need Constructor Parameters using Automapper you need to use the ConstructUsing Method while Creating the Map.
See the Sample Console Applications below.

Sample Console Application C#

using System;
using AutoMapper;

namespace de.Fesslersoft.AutomapperConstructorTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Mapper.CreateMap<Item, Product>()
                .ConstructUsing(x => new Product(x.Id,x.Price)) //without this line there will be an System.Argumentexception
                .ForMember(destination => destination.Number, x => x.MapFrom(source => source.Id))
                .ForMember(destination => destination.Price, x => x.MapFrom(source => source.Price));

            var item = new Item {Id = "Testitem", Price = new decimal(145.69)};
            var product = Mapper.Map<Item, Product>(item);

            Console.WriteLine(product.Number); //prints Testitem
            Console.WriteLine(product.Price); //prints 145.69

            Console.Read();
        }

        internal class Item
        {
            public string Id { get; set; }
            public decimal Price { get; set; }
        }

        internal class Product
        {
            public string Number { get; set; }
            public decimal Price { get; set; }

            public Product(string number, decimal price)
            {
                Number = number;
                Price = price;
            }
        }
    }
}

Sample Console Application VB.NET (autoconverted code)

Imports AutoMapper

Namespace de.Fesslersoft.AutomapperConstructorTest
	Class Program
		Private Shared Sub Main(args As String())
			'without this line there will be an System.Argumentexception
			Mapper.CreateMap(Of Item, Product)().ConstructUsing(Function(x) New Product(x.Id, x.Price)).ForMember(Function(destination) destination.Number, Function(x) x.MapFrom(Function(source) source.Id)).ForMember(Function(destination) destination.Price, Function(x) x.MapFrom(Function(source) source.Price))

			Dim item = New Item() With { _
				Key .Id = "Testitem", _
				Key .Price = New Decimal(145.69) _
			}
			Dim product = Mapper.Map(Of Item, Product)(item)

			Console.WriteLine(product.Number)
			'prints Testitem
			Console.WriteLine(product.Price)
			'prints 145.69
			Console.Read()
		End Sub

		Friend Class Item
			Public Property Id() As String
				Get
					Return m_Id
				End Get
				Set
					m_Id = Value
				End Set
			End Property
			Private m_Id As String
			Public Property Price() As Decimal
				Get
					Return m_Price
				End Get
				Set
					m_Price = Value
				End Set
			End Property
			Private m_Price As Decimal
		End Class

		Friend Class Product
			Public Property Number() As String
				Get
					Return m_Number
				End Get
				Set
					m_Number = Value
				End Set
			End Property
			Private m_Number As String
			Public Property Price() As Decimal
				Get
					Return m_Price
				End Get
				Set
					m_Price = Value
				End Set
			End Property
			Private m_Price As Decimal

			Public Sub New(number__1 As String, price__2 As Decimal)
				Number = number__1
				Price = price__2
			End Sub
		End Class
	End Class
End Namespace

Please Share us:
Please rate this snippet

Leave a Reply