How to construct destination type via constructor while using mappers for each parameter?

72 views
Skip to first unread message

epitka

unread,
Apr 29, 2016, 11:36:06 AM4/29/16
to AutoMapper-users
I've asked this question on stack-overflow also, hopefully I'll get answer here faster :)

Is there a way to map through a constructor while using a mappers for each of the parameters. For example if I have a dto

   public class CreateBid
   {
      public int ClientId { get; set; }
      public int RegulatoryBodyId {get;set;}
      public string Descripption {get;set;
    }

and command

   public class CreateBidCommand
   {
      public CreateBidCommand(Client client, 
             RegulatoryBody regulatoryBody, string description)
      {
           Client = client;
           RegulatoryBody = regulatoryBody;
           Description = description;
      }

      public Client Client {get;}
      public RegulatoryBody RegulatoryBody {get;}
      public string Description {get;}
  }

and I have setup automapping based on the suffix so that for example RegulatoryBodyId is mapped to instance RegualtoryBody,

I have all working if I make properties on my command public, which is not what I want.

What I want is for automapper to use constructor, and see that on source I have ClientId, in constructor I have a instance of Client, so use map to map int=>typeInstance. For all other members that are not in constructor use property mapping. Is this possible?

Just to re-iterate following works

_mapper.Map<int,Client>(112313);

or

_mapper.Map<string,SomeType>("someCode");

What I want is to apply this to every matching member in the constructor, based on the suffixes defined in the profile.

Jimmy Bogard

unread,
Apr 29, 2016, 12:19:23 PM4/29/16
to automapper-users
Hm. So there are suffixes recognized for properties and methods, but not for ctor arguments.

Right now it won't do what you ask, can you open a GH issue for recognizing pre/postfixes in ctor args?

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

epitka

unread,
Apr 29, 2016, 1:54:18 PM4/29/16
to AutoMapper-users
Opened 

#1253

Michael Powell

unread,
Apr 29, 2016, 6:42:51 PM4/29/16
to AutoMapper-users
I've been doing some work with the new lighter weight configuration based mappers. Good stuff in and of itself.

I wonder, you might be able to shoe-horn in a Ctor/MethodInfo into a (synchronized?) configuration and have it spit out the expected mapper?

HTH

Jimmy Bogard

unread,
Apr 30, 2016, 12:46:20 AM4/30/16
to automapp...@googlegroups.com
What do you mean? Intrigued.
--

Michael Powell

unread,
Apr 30, 2016, 11:19:39 AM4/30/16
to AutoMapper-users


On Saturday, April 30, 2016 at 12:46:20 AM UTC-4, Jimmy Bogard wrote:
What do you mean? Intrigued.

Well, I implemented:

interface ICreatableMapper
{
    IMapper CreateMapper();
    IMapper CreateMapper(Func<Type, object> serviceCtor);
}

Could do something like this:

interface ICtorAwareCreatableMapper
{
    IMapper CreateMapper(ConstructorInfo ctorInfo);
    IMapper CreateMapper(ConstructorInfo ctorInfo, Func<Type, object> serviceCtor);
}

And which you pull together like so:

interface IMyMapperConfiguration : IMapperConfiguration, ICtorAwareCreatableMapper
{
}

I call CreateMapper(...) from DI-registered ctor-injected configurations.

private readonly IMyMapperConfiguration _mapper;

public MyServiceOrController(IMyMapperConfiguration mapperConfig)
{
    this._mapper = mapperConfig.CreateMapper(...);
}

I will neither bore anyone to death with implementation details, nor insult the OP's intelligence. I'm sure he can figure it out from there.

To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages