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

Asked

Viewed 750 times

4

I need to return in a REST data of two tables, to be consumed in an Android/IOS App, developed with Xamarin. As I return a DTO, I thought to bring in this DTO data of two tables, but I find this somewhat Gambi. The other solution would be to return two DTO, then two services, one filling the Listview of Release and the other filling the Listview of Items. I don’t know what would be the best approach. 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;
        }

3 answers

4


There is no limit to an DTO representing only one entity, the purpose of the DTO is to transfer an object. If your query brought two records, nothing more fair than 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 of connecting with the bank, serialize, etc.) that you will be doing being that you can bring all in one. As a general rule with some exceptions, doing JOIN is always more performative than doing several queries.

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

  • The problem is in the fields float or double. It gives me this error: The specified cast from a materialized 'System.Double' type to the 'System.Single' type is not valid.

  • Problem? You didn’t mention any cast problem in the question.

  • I know, for the posted question is settled. The problem is when I put fields float, a problem that I was passing, disappeared, I thought I had solved and now started again, so the comment. As for the answer to that question, okay, it’s solved.

3

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

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

When the second solution suggested, depending on its implementation may require two remote calls, you would be subject to latency problems inherent to a remote communication/queries, which increases the response time, since it would be two services.

In short, the use of 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.

1

DTO or Data Transfer Object or Transfer Object is a model 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 to group a set of information so that you do not have to make multiple calls to popular a VIEW or GRID, IE, you are not doing gambiarra, 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 to solve certain problems, follows:

Here is a question about DTO at Sopt: What is a DTO?

Here is a tutorial from Microsoft 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.