Cast from DTO to entity

Posts   
 
    
Luca75
User
Posts: 37
Joined: 10-Feb-2012
# Posted on: 12-May-2016 20:23:06   

Hello I 'm trying to build an WebAPI using version 5 of LLBLGen . I was having problems with serialization in JSON especially in POST .. so I tried to create DTO for my entity I had never done and I remained surprised by how easy it is to do it from your designer..Compliments! I would like to know which is the best way to convert DTO objects EntityClass , both individual objects is List of DTO in List < Entity > and vice versa . I could not find the API to make a cast safe and fast .. The DTO I want to turn are derived from the Entities should be able to cast a safe? I saw on the forum that someone solved by copying the fields one at a time .. I hope there is a solution more efficient .. Thanks for your help.


public void Post([FromBody]JToken jsonBody)
{
List<Dtocustomers> customers = new List<Dtocustomers>();
customers = (List<Dtocustomers>)JsonConvert.DeserializeObject(MyJonsonBoby, typeof(List<Dtocustomers>));//Working Ok
using (var adapter = new DataAccessAdapter())
            {
              ???
               How do I turn List < DTOCustomer > in List < CustomerEntity >
                adapter.SaveCollection(CustomerEntity );
              }         
 }


daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-May-2016 05:04:06   

Luca75 wrote:

I saw on the forum that someone solved by copying the fields one at a time .. I hope there is a solution more efficient .

DTO's lack information about the entity state (fetched, dirty, modified, etc). So the safer mode to do what you want is converting them to entities manually (copying fields, etc) before calling a .Save... method.

David Elizondo | LLBLGen Support Team
Luca75
User
Posts: 37
Joined: 10-Feb-2012
# Posted on: 13-May-2016 11:35:19   

Hello thank you, I will write a class that does this work .. would be nice if in the framework you built a utility class that deals with transforming DTO in the Entity and vice versa . I think it would look good on the class DataAdapter .. I think it will be useful to all users who develop REST services . Thanks and good job!