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

4

I need to return on a REST data from two tables, to be consumed in an Android / IOS App, developed with xamarin. As I return a DTO, I found it good to bring data from two tables in this DTO, but I find it 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 out the Listview of Items. I do not 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 DTO into 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;
        }
    
asked by anonymous 31.08.2017 / 13:56

3 answers

4

There is no limit that a DTO represents only one entity, the goal of the DTO is to transfer an object. If your query brought two records, it's only fair that your DTO represents both. Syntax Imagine the SQL language, when I do a JOIN in sql I do not put within my SELECT both JOIN properties?

On two services, there are two queries (and two process of connecting to the database, serializing, etc.) that you will be doing being that you can bring all into one. As a general rule, with few exceptions, doing JOIN is always more performative than doing multiple queries.

I recommend: link

    
31.08.2017 / 14:30
3

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

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

As long as the second solution suggested, depending on your implementation may require two remote calls, you would be subject to latency problems inherent in a remote communication / queries, which increases response time, since they 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.

    
31.08.2017 / 14:33
1
DTO or Data Transfer Object or Transfer Object is a template 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 for you to group a set of information so you do not have to make multiple calls to populate a VIEW or GRID, ie you're not messing around, this is a normal way to manipulate or traffic data.

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

  • Remove Circular References. ( link )
  • Hide some properties you do not want to make explicitly available.
  • Decoupling the service layer from the database.

Here's a question about DTO in SOpt: What is a DTO?

Following is a tutorial from Microsoft where it is suggested to use DTO:

link

    
31.08.2017 / 15:05