Doubt with public class Listatbproductodto : List<tbProdutDTO> what is it for?

Asked

Viewed 78 times

0

I have this DTO class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


public class tbProdutoDTO
{
public tbProdutoDTO()
{
    this.dsDescricao = "";
    this.dsFornecedor = "";
}

public tbProdutoDTO(int idproduto, String dsdescricao, String dsfornecedor, float vlvalor, int qtestoque)
{
    this.idProduto = idproduto;
    this.dsDescricao = dsdescricao;
    this.dsFornecedor = dsfornecedor;
    this.vlValor = vlvalor;
    this.qtEstoque = qtestoque;
}

public int idProduto { get; set; }
public String dsDescricao { get; set; }
public String dsFornecedor { get; set; }
public float vlValor { get; set; }
public int qtEstoque { get; set; }

}

Now why do I need this class? Follow class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


public class ListatbProdutoDTO : List<tbProdutoDTO>
{
  public ListatbProdutoDTO()
  {
  }
}

I can’t understand what that list class is for.

  • 1

    What do you want to do?

  • I did not understand this Eneric there as list. Make a sale project.... This class I took from a friend...

1 answer

2


You not "need" of it really, even because it is empty and does not implement any functionality. That is, in the case you presented, use List<tbProdutoDTO> or use ListatbProdutoDTO would result in the same thing.

But when creating a class that has List<T> as a basis is useful?

Well, imagine that within a system you always perform some operations on top of collections of class objects Coisa, and not to keep repeating the code of these operations in various places in the system you decided to create various help methods, helpers. The problem is that you now own several helpers who will always work on collections of Coisa, List<Coisa>.

You could group these methods into a separate class to further organize the code, but you can do something better, you can create a custom collection, a collection of Coisa that implements these helpers.

Can you imagine the result? Your code has become more organized now.

Below I worked out an example to make it clearer what I said:

class Program
{
    static void Main(string[] args)
    {
        // Criação de alguns dados de exemplo
        ListCoisa list = new ListCoisa
        {
            new Coisa { Id = 1, Descricao = "Coisinha", Preco = 10 },
            new Coisa { Id = 2, Descricao = "Coisa Normal", Preco = 20 },
            new Coisa { Id = 3, Descricao = "Coisona" , Preco = 30 }
        };

        // Utilizando o método CalcularTotal a partir do objeto ListCoisa
        Console.WriteLine(list.CalcularTotal());
        // Saída na tela: 60

        Console.ReadKey();
    }
}

class Coisa
{
    public int Id { get; set; }
    public string Descricao { get; set; }
    public decimal Preco { get; set; }
}

class ListCoisa : List<Coisa>
{
    public decimal CalcularTotal()
    {
        decimal total = 0;

        // Me referindo à coleção em si através da palavra-chave this
        foreach (Coisa coisa in this)
        {
            total += coisa.Preco;
        }

        return total;
    }
}

Browser other questions tagged

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