C # - Automapper :: convert object to a complex object

0

I have the following Request and Response structure:

public class Action
{
    public class Create
    {
        public class Request
        {
            public string operationdate { get; set; }
            public string action { get; set; }
            public string identificationaccount { get; set; }
            public string observation { get; set; }
            private ErrorModel.Error errorrow = new ErrorModel.Error();
            public ErrorModel.Error ErrorRow { get { return errorrow; } set { errorrow = value; } }
        }
        public class Response : Request
        {
            public Int32 version { get; set; }
            public string groupcode { get; set; }
            public bool active { get; set; }
        }
    }
}

and I want to map to the following DTO structure:

public class GroupDTO
{
    public Int64 idGroup { get; set; }
    public string GroupCode { get; set; }
    public string GroupDate { get; set; }
    public string Obs01 { get; set; }
    public string Obs02 { get; set; }
    public string Obs03 { get; set; }
    public string Obs04 { get; set; }
    public bool Active { get; set; }
    private List<ActionModelDTO> _Actions = new List<ActionModelDTO>();
    public List<ActionModelDTO> Actions { get { return _Actions; } set { _Actions = value; } }
}

public class ActionModelDTO
{
    public Int64 idAction { get; set; }
    public string ActionDate { get; set; }
    public Int32 Version { get; set; }
    public Int32 idGroup { get; set; }
    public bool Active { get; set; }
    private ActionDetailModelDTO _ActionDetailModel = new ActionDetailModelDTO();
    public ActionDetailModelDTO ActionDetailModel { get { return _ActionDetailModel; } set { _ActionDetailModel = value; } }
    private ActionUserDetailModelDTO _ActionUserDetailModel = new ActionUserDetailModelDTO();
    public ActionUserDetailModelDTO ActionUserDetailModel { get { return _ActionUserDetailModel; } set { _ActionUserDetailModel = value; } }
}

public class ActionDetailModelDTO
{
    public Int64 idActionDetailModel { get; set; }
    public Int32 idActionType { get; set; }
    public string ActionType { get; set; }
}

public class ActionUserDetailModelDTO
{
    public Int64 idActionUserDetailModel { get; set; }
    public string UserAccount { get; set; }
    public string UserObservation { get; set; }
    public string IdentificationNumber { get; set; }
}

The mapping would be as follows:

  • Action.Create.Request.operationdate = > ActionModelDTO.ActionDate
  • Action.Create.Request.action = > ActionDetailModelDTO.ActionType
  • Action.Create.Request.identificationaccount = > ActionUserDetailModelDTO.UserAccount
  • Action.Create.Request.observation = > ActionUserDetailModelDTO.UserObservation
  • The object to which you would have to map is GroupDTO ... it is the first time I use Automapper, and it returns the following error:

      

    Unmapped members were found. Review the types and members below. Add a   custom mapping expression, ignore, add to custom solve, or modify   the source / destination type For no matching constructor, add a no-arg   ctor, add optional arguments, or map all of the builder parameters

    The fields that do not appear in the mapping that I specified are completed after the object persists in DB.

        
    asked by fdarle 18.12.2018 в 16:31
    source

    1 answer

    0

    To do what you need you must first configure the classes (and properties if applicable) of the objects you want to convert, like this:

    using AutoMapper;
    
    public class TuClase
    {
       private MapperConfiguration config;
       private IMapper mapper;
    
       public Program()
       {
          this.config = new MapperConfiguration(m =>
          {
             m.CreateMap<Action.Create.Request, ActionModelDTO>()
                .ForMember(to => to.ActionDate, from => from.MapFrom(x => x.operationdate));
             });
             this.mapper = config.CreateMapper();
          }
       }
    }
    

    You will notice that for the properties of your Action.Create.Request and ActionModelDTO object to take the corresponding value you must explicitly specify these attributes.

    Finally to move from one object to another you have to perform the corresponding mapping in this way

    public void MiMetodo()
    {
       Action.Create.Request item = new Action.Create.Request
       {
          action = "test",
          observation = "mensaje",
          identificationaccount = "otro",
          operationdate = "22-09-2024"
       };
    
       ActionModelDTO item2 = mapper.Map<ActionModelDTO>(item);
    }
    

    It is worth mentioning that if you want to do your mapping upside down you should only add in the configuration

    m.CreateMap<ActionModelDTO, Action.Create.Request>()
    

    which is nothing else, that the objects upside down;)

    You tell us how you are doing =)

        
    answered by 18.12.2018 в 19:55