Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Transfer Objects - do mapping in DTO, or in business object?

I have a WCF service and have just created a DTO for a business object.

My question is where to put the mapping between the two?

A) In the DTO?

public class PersonDTO
{
    [DataMember] public string Id              { get; set; }
    [DataMember] public string Name            { get; set; }

    public void CloneFrom(Person p)
    {
        Id   = p.Id;
        Name = p.Name;
    }

    public void Populate(Person p)
    {
        p.Id   = Id;
        p.Name = Name;
    }
}

or

B) In the business object?

public class Person
{
    public string Id              { get; set; }
    public string Name            { get; set; }

    public void CloneFrom(PersonDTO dto)
    {
        Id   = dto.Id;
        Name = dto.Name;
    }

    public PersonDTO GetDTO()
    {
        return new PersonDTO()
        {
            Id   = Id;
            Name = Name;
        }
    }
}

I like the separation of concerns in A (Business object has no knowledge of DTOs), but I prefer the encapsulation of B (no need to expose business object guts to DTO).

Just wondered if there was a standard way?

like image 538
GazTheDestroyer Avatar asked Apr 24 '12 13:04

GazTheDestroyer


People also ask

What is the point of using DTO data transfer objects?

A data transfer object (DTO) is an object that carries data between processes. You can use this technique to facilitate communication between two systems (like an API and your server) without potentially exposing sensitive information. DTOs are commonsense solutions for people with programming backgrounds.

Is DTO business object?

DTO is used to transfer data between layers/tiers. For such purpose it doesn't need any methos and sometimes it even should not have any methods - for example when DTO is exposed over web service. Business object is clever object which contains data and methods which performs operations (change data) on this object.

When should I use a DTO?

A DTO is helpful whenever you need to group values in ad hoc structures for passing data around. From a pure design perspective, DTOs are a solution really close to perfection. DTOs help to further decouple presentation from the service layer and the domain model.

What is domain object and DTO data transfer?

A Data Transfer Object (DTO) is an object intended to carry data, for example between the client and the server or between the UI and the domain layer. It exposes fields or properties (getters and setters) publicly. Sometimes a DTO could be seen as an anemic model.


2 Answers

i'd think this calls for a separate class, as neither the BO nor the DTO should be concerned with their transformation into another class.

I personally use the automapper library for object transformations. With simple transformations like in your example the mapping is done in a single line of code, complex transformations are also easy to set up.

If you want to map yourself, you could still use extension methods to keep the mapping implementation separated from your DTO and BO classes.

like image 142
Dirk Trilsbeek Avatar answered Sep 21 '22 13:09

Dirk Trilsbeek


I would suggest a Component Layer. It should be responsibly for communication between your business layer and your data layer. In this case, you can use it to translate your DTO objects to Business Objects.

like image 27
Khan Avatar answered Sep 23 '22 13:09

Khan