0

I have domain model like this

public class EntityOne
    {
        public int EnityOneId { get; set; }
        public int EntityOnePropertyOne { get; set; }

        public List<EntityTwo> EntityTwos { get; set; }
    }

    public class EntityTwo
    {
        public int EntityTwoId { get; set; }
        public string EntityTwoPropertyOne { get; set; }

        public int EntityThreeId { get; set; }
        public int EnityOneId { get; set; }

        public virtual EntityOne EntityOne { get; set; }
        public virtual EntityThree EntityThree { get; set; }
    }

    public class EntityThree
    {
        public int EntityThreeId { get; set; }
        public string EntityThreePropertyOne { get; set; }
    }

and I have DTO like this

public class EntityDTO
    {
        public int EntityOnePropertyOne { get; set; }
        public string EntityThreePropertyOne_ValueOne { get; set; }
        public string EntityThreePropertyOne_ValueTwo { get; set; }
        public string EntityThreePropertyOne_ValueThree { get; set; }
        public string EntityThreePropertyOne_ValueFour { get; set; }
        public string EntityThreePropertyOne_ValueFive { get; set; }
    }

I want to configure mapping from DTO to DomainModel and the reverse using AutoMapper but I didnt know how to do that... any suggestion or help

Mosby
  • 53
  • 1
  • 8

1 Answers1

0

I'm not sure what you're trying to accomplish here.

I get that you want to map to EntityDTO, but from what other type? I will assume you want to use EntityTwo as the source.

In that case,

  • EntityOnePropertyOne: Will be obtained automatically via Flattening from the source (EntityTwo) - So, no problem here.
  • EntityThreePropertyOne_ValueOne: This will assume you have a property called EntityThree (which you do), and within that type, a property called PropertyOne_ValueOne of type int (which you don't). Same applies for the rest.

The other way around will get trickier, since I see there will be lots of properties ignored, so you need to tell AutoMapper, that you don't want it to be concerned about all that bunch of properties in your complex type, that don't come from the DTO.

  • Sorry it is not "EntityThreePropertyOne_ValueOne" is not int it is string! How could I use automapper to map those value. The challenge for me is to change the property value's of dto to Item of Entity one, which holds many to many relationship with additional field – Mosby Mar 27 '14 at 16:46