c# - AutoMapper Profiles: flattening a dto for mapping -


i have main class has nested class. have used map using mapper class

public class main {     public string name { get; set; }     public list<quantitylocation> nc { get; set; } }  public class nestedclass {     public decimal b { get; set; }     public string { get; set; } }  public class flattened {     public string name { get; set; }      public string { get; set; }     public decimal b { get; set; } } 

mapping done using mapper class below.

    mapper.createmap<nestedclass, flattened>();     mapper.createmap<main, flattened>();     mapper.createmap<main, list<flattened>>()         .convertusing(i =>                 i.nc.select(                     flat =>                     {                         var flatlist = mapper.map<flattened>(i);                         mapper.map(flat, flatlist);                         return flatlist;                     }).tolist()); 

now when moved mapping profile class, had change lines above below:

createmap<nestedclass, flattened>(); createmap<main, flattened>(); createmap<main, list<flattened>>()     .convertusing(i =>             i.nc.select(                 flat =>                 {                     var flatlist = mapper.map<flattened>(i);                     mapper.map(flat, flatlist);                     return flatlist;                 }).tolist()); 

the problem facing how convert these 2 lines below in snippet above.

               var flatlist = mapper.map<flattened>(i);                 mapper.map(flat, flatlist); 

you see injecting mapper.engine constructor of controllers. earlier using static mapper class used called in global.asax. error 1 below.

missing type map configuration or unsupported mapping.  mapping types: main -> list`1 myproj.main -> system.collections.generic.list`1[[myproj.flattened, myproj, version=1.0.0.0, culture=neutral, publickeytoken=null]]  destination path: list`1  source value: myproj.main 

the proper way use custom type converter, in inject mapping engine itself. , use constructservicesusing in profile declaration.

assuming use of ioc container, , have registered in mapping engine, , custom type converter, inside converter use engine.map instead static mapper.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -