Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a proper use of DTO?

I'm writing a console application that does a good amount of data retrieval from stored procedure recordsets. For each recordset type I'm working with, I have a Repository that uses EF with custom complex types to retrieve the data:

public interface IBalanceSheetRepository
{
    IEnumerable<BalanceSheetRecordDTO> GetBalanceSheetRecords();
}

public class BalanceSheetRepository : IBalanceSheetRepository
{
    DBContext _context;

    ...

    public IEnumerable<BalanceSheetRecordDTO> GetBalanceSheetRecords()
    {
        ObjectResult<BalanceSheetRecord> results = _context.GetBalanceSheet();
        return results.Select(CreateBalanceSheetDTOFromDAO);
    }

    private static BalanceSheetRecordDTO CreateBalanceSheetDTOFromDAO(BalanceSheetRecord dao)
    {
        return new BalanceSheetRecordDTO { ... };
    }
}

Here, BalanceSheetRecord is a complex data type that I created in the designer. I created a DTO to avoid coupling since the BLL should not know about the BalanceSheetRecord type.

Here's my question: Since the DTO type is used in the method signature of the repository interface, and since my BLL will ultimately use the repository & be returned a collection of the DTOs, it seems to be a cross-cutting concern. Therefore I have the DTO living in a separate "Infrastructure" assembly, along with the repo interfaces. Is this good practice for what I'm trying to achieve, or did I take a wrong turn somewhere?

Also: Is it bad practice to new up the data context in my repository? Is some amount of coupling OK when both components belong to the DAL? I want to use DI but it seems more useful for swapping out the DBContext implementation of the repo for a TestBalanceSheetRepository, for instance.

like image 829
Chris Trombley Avatar asked Jun 10 '11 18:06

Chris Trombley


People also ask

When should DTO be used?

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.

Where should DTO be used?

DTOs are most commonly used by the Services layer in an N-Tier application to transfer data between itself and the UI layer. The main benefit here is that it reduces the amount of data that needs to be sent across the wire in distributed applications. They also make great models in the MVC pattern.

Why we are using 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 DTO means?

In the field of programming a data transfer object (DTO) is an object that carries data between processes. The motivation for its use is that communication between processes is usually done resorting to remote interfaces (e.g., web services), where each call is an expensive operation.


1 Answers

I prefer to return actual entities from my repositories. This way when you need to orchestrate complex interactions between different entities in the service layer, there's no converting back and forth to DTO's. The service layer then projects entities into DTO's when returning data to the application layer.

I know there are purists who say that all interaction between layers should be done with DTO's, but I find that impractical. The service layer will end up coupled to the entities anyway, so it's not like you're adding coupling.

It also limits the projection/flattening of DTO's to entities to the service layer. Which to me is a plus since these activities add to complexity and decreases performance.

Your data context is your unit of work. The "one unit of work per repository" idea is an anti-pattern. Units of work should be scoped by the service layer, which may involve 1-many entities from 1-many repositories. If each repository has a different unit of work, you lose your ability to have service layer calls maintain consistent transactions easily.

like image 60
Brook Avatar answered Sep 28 '22 01:09

Brook