Search code examples
c#mappingvalueinjecter

Map DTO to Domain Model


I have a DTO as follows:

 class ProjectDTO{

     string Title;
     string Data;

 }

Then domain model as follows:

  class Project{
    Content Content {set;get;}

  }

  class Content{
     string Title
     string Data;

  }

I am using ValueInjecter. I want to Map DTO to Domain Model.

project.InjectFrom(projectDTO);

Doesnt quite work due to inner object.

How can this be done using value injecter?


Solution

  • Looking at the documentation, I think you need to use the FlatLoopValueInjection type.

    project.InjectFrom<FlatLoopValueInjection>(projectDTO);
    

    Source: https://valueinjecter.codeplex.com/wikipage?title=flattening&referringTitle=Home

    edit

    I just noticed that this only solves the exact opposite of your problem. Heh.

    edit

    Why not this?

    project.Content.InjectFrom(projectDTO);
    

    edit

    There is also the UnflatLoopValueInjection type that seems to do what you need.

    https://valueinjecter.codeplex.com/wikipage?title=unflattening

    The documentation is a little vague though. Seems that you have to follow some sort of naming convention in order for it to actually do anything.

    class ProjectDTO
    {
        string ContentTitle; // Project.Content.Title becomes ProjectDTO.ContentTitle
        string ContentData;  // Project.Content.Data becomes ProjectDTO.ContentData
    }
    

     

    project.InjectFrom<UnflatLoopValueInjection>(projectDTO);