c# - How do I map a model with nested properties to a flat Dto? -


I am trying to make mapping from my unit model to a DTO, but every time I am trying to create it I am Mapping

I have two domain classes. It is a simplification of our model (for example, devices are many properties that represent a group of different things) :

  class device {public entity name}; Set;}} Class DeviceAccessToken {public device device {get; Set;} public string key {get; Set;} public string secret {get; Set}}}   

Then I want to map DeviceAccessToken to this DeviceDto (also simple, most of the fields in it) Exists in the original device model):

  class DeviceDto {public int name {get; Set;} public string key {get; Set;} public string secret {get; Set;}}   

Is there a way to make this mapping in mapping without specifying all areas of the device domain model?

This is effectively represented by automaker profiles:

  Category DeviceMappingProfile: Profile {Secure Override Zero Configure () {this.CreateMap & lt; DeviceAccessToken, DeviceDto & gt; (); this. Createmap & lt; Device, DeviceDto & gt; (). For mail (dest => dest.Key, opt = & gt; opt.Ignore ()) for mail (dest = & gt; dest.Secret, opt => opt.Ignore ( )); }}   

To do this, the .AllMembers call was an unsuccessful attempt, it should not work as I saw.

I understand that I can do this by specifying each property in the device DeviceAccessToken-> DeviceDown mapping, but it is a nightmare and lots It would be redundant because the names are the same.

"Whether to create this mapping without explicitly specifying all the fields of the device Is there a way in which domain model in mapping? "

Yes, you can use naming conventions in your DTO object and it will allow you to enter them in the map form.

As an example:

Your values ​​are in the key and in the DeviceAccessToken and DeviceDto , to map them Will not be required. As the device is a nested object, your DTO can use the DeviceName conference.

Example:

 Using the  system; Using Automapers; Class device {public string name {get} Set;}} Class DeviceAccessToken {public device device {get; Set;} public string key {get; Set;} public string secret {get; Set;}} Class DeviceDto {public string DeviceName {get; Set;} public string key {get; Set;} public string secret {get; Set;}} Public Class Program {Public Zero Main ()} {// Automapper Mapper Configure Caterpap & lt; DeviceAccessToken, DeviceDto & gt; (); Var dat = new DeviceAccessToken {device = new device} {name = "dev name"}, key = "key", secret = "secret"}; Var dto = Mapper Map & lt; DeviceDto & gt; (DAT); Console.WriteLine (dto.DeviceName); Console.WriteLine (dto.Key); Console.WriteLine (dto.Secret); }}   



Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -