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 rate this snippet

How to Map to Protected Property using AutoMapper in C# and VB.NET

To map protected Properties using AutoMapper in C# and VB.NET you can use the following snippet.

Samples

Samples C#

Samples VB.NET

🙁 missing, feel free to provide a VB.NET sample

Another approach would be to create a method in the destination object to do the mapping.

Samples

Sample C#

Sample VB.NET

 

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5not working .NET 4.6

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

 

Please rate this snippet

How to configure Conditional Mapping in AutoMapper in C# and VB.NET

To configure Conditional Mapping in AutoMapper in C# and VB.NET you can use the following snippet.

Sample C#

 Mapper.CreateMap<Source, Target>().ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.Variable1 == "Value1" ? true : false));

Sample VB.NET

Mapper.CreateMap(Of Source, Target)().ForMember(Function(dest) dest.Value, Function(opt) opt.MapFrom(Function(src) If(src.Variable1 = "Value1", True, False)))

Please rate this snippet