Is it correct in a DTO class to have attributes of two or more tables?

I need to return in a REST data from two tables, to be consumed in an Android/IOS App, developed with xamarin. As I return a DTO, I thought it was good to bring in this DTO data from two tables, but I think this is somewhat gambi. The other solution would be to return two DTOs, there would be two services, one filling the release Listview and the other filling the item Listview. I don't know what the best approach would be. Below my DTO.

public class LiberacaoItensDTO
    {
        //Mapeamento dos campos, pois estava dando erro de cast e assim resolveu
        public LiberacaoItensDTO()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                .ForMember(d => d.DataLib, opt => opt.MapFrom(src => DataLib.ToString()));
            });
        }

        //Dados da tabela Liberação
        public int IdLiberacao { get; set; }
        [DefaultValue(0)]
        public int IdOrcamento { get; set; }
        [DefaultValue(0)]
        public int IdVendedor { get; set; }
        public string Vendedor { get; set; }
        public int IdFilial { get; set; }
        public string Filial { get; set; }
        [DefaultValue(0)]
        public float? DataLib { get; set; }
        public int IdCliente { get; set; }
        public string Cliente { get; set; }
        public string TipoVenda { get; set; }
        [DefaultValue(0)]
        public float Juros { get; set; }
        [DefaultValue(0)]
        public float Desconto { get; set; }
        [DefaultValue(0)]
        public double Vencimento { get; set; }
        [DefaultValue(0)]
        public double Acrescimo { get; set; }
        [DefaultValue(0)]
        public float Entrada { get; set; }

        //Dados da tabela de ItensLib
        public int IdProduto { get; set; }
        public string Produto { get; set; }
        [DefaultValue(0)]
        public float Qtde { get; set; }
        [DefaultValue(0)]
        public float Unitario { get; set; }
        [DefaultValue(0)]
        public float CustoDiario { get; set; }
        [DefaultValue(0)]
        public double UltCondicao { get; set; }
        [DefaultValue(0)]
        public float Total { get; set; }
    }

Method to bring the DTO in service

public List<LiberacaoDTO> getAutoriza(int idorcamento)
        {

            var lista = contexto.Liberacoes??????? Faria um Join com as duas tabelas, liberacao e itenslib
                        .Where(lib => lib.IdOrcamento == idorcamento)
                        .Select(lib => new LiberacaoItensDTO
                        {
                            //Aqui coloco os campos retornados
                        }).ToList();
            return lista;
        }
Author: pnet, 2017-08-31

3 answers

There is no limit to a DTO representing only one entity, the purpose of the DTO is to transfer an object. If your query brought two records, nothing more fair that your DTO represents both. Imagine syntactically the SQL language, when I do a join in sql I do not put inside my SELECT both join properties?

About two services, are two queries (and two process connect with the bank, serialize, etc.) that you will be doing being that you can bring all in one. In general, with a few exceptions, joining is always more performant than doing multiple queries.

Recommend: https://dba.stackexchange.com/questions/42998/are-individual-queries-faster-than-joins

 4
Author: Gabriel Coletta, 2017-08-31 12:30:58

DTO is an object created to transport data and reduce the number of remote calls.

So there's no problem bringing data from two tables into your DTO (it's not gambiarra :-)). Quite the contrary, doing this way will be performed a single query/remote call instead of two.

As for the second suggested solution, depending on your implementation may require two remote calls, you would be subject to latency issues inherent in one remote communication/queries, which increases the response time, since it would be two services.

In short, using a single DTO in your scenario is more interesting to reduce the number of remote calls, while the second option goes entirely in the opposite direction.

 3
Author: Renan, 2017-08-31 12:45:19

DTO or Data Transfer Object or Transfer Object is a model that you define for transporting data between different components of a system, different instances or processes of a distributed system or different systems via serialization or even via network / webservices.

The idea is you Group a set of information so you don't have to make multiple calls to populate a VIEW or GRID, i.e. you're not doing gambiarra, that's a normal way to manipulate or traffic data.

In addition to grouping the data as said above, there are other details where, using DTO's helps us solve certain problems, follows:

Follows a question about DTO not SOpt: what is a DTO?

Follows a Microsoft tutorial where it is suggested to use DTO:

Https://docs.microsoft.com/pt-br/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

 1
Author: mcamara, 2017-08-31 13:05:21