131

In cases where my destination setters are private, I might want to map to the object using the destination object's constructor. How would you do this using Automapper?

d219
  • 2,707
  • 5
  • 31
  • 36
jlembke
  • 13,217
  • 11
  • 42
  • 56

5 Answers5

174

Use ConstructUsing

this will allow you to specify which constructor to use during the mapping. but then all of the other properties will be automatically mapped according to the conventions.

Also note that this is different from ConvertUsing in that convert using will not continue to map via the conventions, it will instead give you full control of the mapping.

Mapper.CreateMap<ObjectFrom, ObjectTo>()
    .ConstructUsing(x => new ObjectTo(arg0, arg1, etc));

...

using AutoMapper;
using NUnit.Framework;

namespace UnitTests
{
    [TestFixture]
    public class Tester
    {
        [Test]
        public void Test_ConstructUsing()
        {
            Mapper.CreateMap<ObjectFrom, ObjectTo>()
                .ConstructUsing(x => new ObjectTo(x.Name));

            var from = new ObjectFrom { Name = "Jon", Age = 25 };

            ObjectTo to = Mapper.Map<ObjectFrom, ObjectTo>(from);

            Assert.That(to.Name, Is.EqualTo(from.Name));
            Assert.That(to.Age, Is.EqualTo(from.Age));
        }
    }

    public class ObjectFrom
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    public class ObjectTo
    {
        private readonly string _name;

        public ObjectTo(string name)
        {
            _name = name;
        }

        public string Name
        {
            get { return _name; }
        }

        public int Age { get; set; }
    }
}
Pavel Jounda
  • 219
  • 5
  • 14
Jon Erickson
  • 112,242
  • 44
  • 136
  • 174
  • I'm guessing "ConstructUsing" must be in a newer version of automapper than the one we're using. Thanks Jon – jlembke Feb 10 '10 at 19:33
  • 9
    Much thanks for this example Jon. "ConstructUsing" is great! Allows me to keep my DTOs immutable with setters marked as private. – Daniel Nov 08 '10 at 00:46
  • 3
    Works a treat for me; AutoMapper currently doesn't like constructors where all parameters are optional, so I just use .ConstructUsing(x => new MyClass()); – David Keaveny Apr 11 '11 at 23:56
  • How to do it with non-generic CreateMap method? – dariol Jul 11 '11 at 22:41
  • 1
    Tried something similar Mapper.CreateMap().ConstructUsing(x => new OrderViewModel(this)); ... Get a "The call is ambiguos" compiler error – Chris Klepeis Mar 29 '12 at 17:49
  • @ChrisKlepeis it's giving you that error because it doesn't know what "this" is referring to when attempting to map between the types. you want to do .ConstructUsing(x => new OrderViewModel(x)) most likely. – Philter Mar 26 '13 at 13:44
  • Both methods throw "Expression must be writeable Parameter name: left" if you use readonly fields instead of a private setters on your destination object. – Facio Ratio Oct 23 '13 at 22:17
  • 2
    What if I needed to pass something more sophisticated than a `string`? What if `ObjectFrom` contained `ChildObjectFrom` type property which had to be passed to `ObjectTo` constructor? – Gabrielius Oct 08 '15 at 17:23
  • Is this a better answer than @Matthieu's answer? – Patrick Szalapski Oct 25 '16 at 12:58
  • For anyone using this with EF Core - this worked in 2.2, but not in 3.0 due to a bug. If you are on 3.0 you will need to upgrade to 3.1.1 to get the fix - https://github.com/dotnet/efcore/issues/18888 – DanO Mar 26 '20 at 13:52
  • You meant probably that all other **public** properties not present in the constructor will be mapped according to convention. Private properties will not be mapped, so `ConstructUsing` can successfully be used with aggregates. Am I right? – Paul-Sebastian Manole Apr 17 '21 at 11:46
17

The best practice is to use documented approaches from AutoMapper http://docs.automapper.org/en/stable/Construction.html

public class SourceDto
{
        public SourceDto(int valueParamSomeOtherName)
        {
            Value = valueParamSomeOtherName;
        }

        public int Value { get; }
}

Mapper.Initialize(cfg => cfg.CreateMap<Source, SourceDto>()
  .ForCtorParam(
    "valueParamSomeOtherName", 
    opt => opt.MapFrom(src => src.Value)
  )
);
LosManos
  • 7,195
  • 6
  • 56
  • 107
  • 1
    Unfortunately there’s no way to use `nameof` here to avoid brittle hard-coded magic-strings, and (unless AutoMapper now has a custom Roslyn rule) you won’t get a compile-time error if you omit a parameter or specify a parameter that doesn’t exist. – Dai Jun 29 '22 at 09:26
13

You should use the Map method that lets you set the destination. For example :

Mapper.CreateMap<ObjectFrom, ObjectTo>()

var from = new ObjectFrom { Name = "Jon", Age = 25 };

var to = Mapper.Map(from, new ObjectTo(param1));
Pavel Jounda
  • 219
  • 5
  • 14
Matthieu
  • 4,605
  • 4
  • 40
  • 60
7

At the time of writing this answer, AutoMapper will do this automatically (with a simple CreateMap<>() call) for you if the properties match the constructor parameters. Of course, if things don't match up, then using .ConstructUsing(...) is the way to go.

public class PersonViewModel
{
    public int Id { get; set; }

    public string Name { get; set; }
}

public class Person
{
    public Person (int id, string name)
    {
        Id = id;
        Name = name;
    }

    public int Id { get; }

    public string Name { get; }
}

public class PersonProfile : Profile
{
    public PersonProfile()
    {
        CreateMap<PersonViewModel, Person>();
    }
}

Note: This assumes you are using Profiles to setup your automapper mappings.

When used like below, this produces the correct object:

var model = new PersonViewModel
{
    Id = 1
    Name = "John Smith"
}

// will correctly call the (id, name) constructor of Person
_mapper.Map<Person>(model);

You can read more about automapper construction in the offical wiki on GitHub

lxalln
  • 930
  • 11
  • 29
  • Looks like CreateMap<> should be PersonViewModel and not PersonProfile. As well as in the second code block PersonModel should be PersonViewModel. – Ben Sampica Aug 07 '19 at 19:30
5

Personally I always prefer to be as explicit as possible when using AutoMapper to avoid any potential bugs in the future.

If you call the ConstructUsing method just passing the parameters one by one in the good order you might face a bug one day.

What if a developer inverts 2 string parameters or add a new optional parameter before some existing optional parameters? You would get a mapping bug where a property isn't mapped to the destination property it's supposed to. For that reason I prefer to define my mapping using named parameters when instanciating my object.

Here are the different signatures of the ConstructUsing method:

TMappingExpression ConstructUsing(Func<TSource, ResolutionContext, TDestination> ctor);
TMappingExpression ConstructUsing(Expression<Func<TSource, TDestination>> ctor);

In that case we want to use the first one because it's not possible to use named parameters in an Expression Tree (you would get a compilation error an expression tree may not contain a named argument specification).

Here is how to use it:

 CreateMap<FromType, ToType>()
    .ConstructUsing((src, res) =>
    {
        return new ToType(
            foo: src.MyFoo,
            bar: res.Mapper.Map<BarModel>(src.MyBar),
        );
    });

Note the Func's 2nd parameter res which is the Resolution Context. This parameter allows you to use already registered mappings.

Careful though, I'd like to catch your attention on a drawback of declaring mappings with constructors. If your classes don't have public setters (read-only properties or private set) you won't be able to use the following overload of the Map method:

TDestination Map<TSource, TDestination>(TSource source, TDestination destination);

This overload could be very handy when updating an entity with EF Core for example

mapper.Map(updateModel, existingEntity);
await dbContext.SaveChangesAsync();

Thankfully there's another way to update entities with EF Core.

Jérôme MEVEL
  • 7,031
  • 6
  • 46
  • 78
  • Reg "What if a developer inverts 2 string parameters" I have the understanding that the names of the parameter matters, not the order. – LosManos Nov 10 '21 at 07:21
  • 2
    Things I learned today: Having a mapper inside the `ConstructUsing`. – LosManos Nov 10 '21 at 07:22