Modeling - Model

Asked

Viewed 83 times

2

I’m having trouble creating my models.

Let’s imagine that I have 4 different types of products (Here as A,B,C and D)

public class Produto
{
    public int ProdutoId { get; set; }
    public TipoProduto TipoProduto { get; set; } //enum tipo
}

But Type A products (TipoProduto==A) it can be disassembled and with this 4 new pieces are created, in which I also want to register. (until then I will register a model called Productostipoapartes) I thought of creating another Model where I could store the Base Product Id

public class ProdutoTipoA
{
    public int ProdutoTipoAId { get; set; }
    public int ProdutoId { get; set; }
//caracteristicas específicas do tipo A
    public virtual Produto Produto { get; set; }
}

products of type B

public class ProdutoTipoB
{
    public int ProdutoTipoBId { get; set; }
    public int ProdutoId { get; set; }
//caracteristicas específicas do tipo B
    public virtual Produto Produto { get; set; }
}

Is this approach correct? in the Products model I would have some kind of virtual relationship with other types A,B,C or D ?

Or better I create:

public class ProdutoBase
{
    public int ProdutoId { get; set; }
    public TipoProduto TipoProduto { get; set; } //enum tipo
    public string Serial { get; set; }
}

and the Productive, Productosb inherit the basis?

public class ProdutoTipoA : ProdutoBase
    {
    //caracteristicas específicas do tipo A
    }

And in addition, I will have the orders, which will be linked to the Products, so it would be complex if I had several types of Model Products. I don’t know which way to go.


Update: The options I see are:

  • Create Base Class and Products A,B,C and D inherit
  • Create a Product and other products that are hierarchically inferior, as if they were children products.
    • Create 4 separate products and turn my life into madness..
  • why not create a product class with a type property and assigned to an Enum?

  • I fixed a line in the code, you had inherited it ProdutoTipoB in class ProdutoTipoA , I switched to ProdutoBase, I don’t know if it’s correct, if I’m not canceling the edition.

  • she wouldn’t inherit from the productBase

  • @Damondudek already has the Enum for the product type.. see Typoproduct , the problem is that products A,B,C,D have a 6 fields each different, so how to separate the models but maintain a link.. and still Product B will have sub-products...to complicate

  • Would your case be Product that can be disassembled and sold in parts? And those parts you wanted each of them to be a new Product and so on?

  • @Exact Leandrosimões, for the Productoa I can divide it into several parts, but the products B,C do not.. so I do not know if I create 4 independent models? if I inherit them? do I relate one to another?. until pq on request I will store which id? ,rs

  • Hello, If you only need to identify the product type for a simple and small logic, I suggest creating an attribute in the class with the product Enum. Otherwise, in need of a more robust logic, I believe it would be better to have a parent class with the attributes and methods in common and the others inheriting from this class.

  • note that I have already created this Enum public Typeproduct Typeproduct { get; set; } //In a type I want different products that have a certain basis in common I do by inheritance or hierarchy of models?

  • It depends, you have to evaluate if the products can have dependencies among themselves. If not, use the good old heritage. Take a look at this article: http://www.guj.com.br/t/understanding-a-hierarchial-de-classes-extends-implements/103546

Show 4 more comments

1 answer

3


If you had given a more concrete example, you could help better. It seems to me that you are working with structures.

I will make a very superficial example. Using the model below, we will assemble a structure and assemble a pen:

inserir a descrição da imagem aqui

public class Parte
{
    public Guid Id {get;set;}
    public string Nome {get;set;}
    public string Propriedades {get;set;}
    public List<Parte> Partes {get;set;}
    public Parte MontaEm {get;set;}

    public Parte()
    {
        Id = Guid.NewGuid();
        Partes = new List<Parte>();
    }

    public Parte MontarEm(Parte parte, string nome)
    {
        MontaEm = parte;
        parte.Partes.Add(this);

        var novaParte = new Parte
        {
            Nome = nome             
        };
        novaParte.Partes.AddRange(new []  { this, parte});

        return novaParte;
    }
}

Whenever you ride one part into another, a new part is created. This is not simple to control, and one can add many other features, such as which part can connect with which part, whether it can be removable, whether it can be interchangeable, etc.

    var esfera = new Parte
    {
        Nome = "Esfera",
        Propriedades = JsonConvert.SerializeObject( new { Tamanho = 0.1m, Material = "Aluminio" } )         
    };

    var pontaMetalica = new Parte
    {
        Nome = "Ponta Metalica",
        Propriedades = JsonConvert.SerializeObject( new { Material = "Latão", Peso = 0.02m } )
    };

    var pontaClassica = new Parte
    {
        Nome = "Ponta Classica",
        Propriedades = JsonConvert.SerializeObject( new { Material = "Plastico", Comprimento = 0.3m } )
    };

    var cartucho = new Parte
    {
        Nome = "Cartucho",
        Propriedades = JsonConvert.SerializeObject( new { Comprimento = 0.5m, Cor = "Amarela" } )
    };

    var tinta = new Parte
    {
        Nome = "Tinta",
        Propriedades = JsonConvert.SerializeObject( new { Cor = "Azul", Volume = 2m } )
    };

    var pontaEsferografica = esfera.MontarEm(pontaMetalica, "Ponta Esferografica");

    var pontaCompleta = pontaEsferografica.MontarEm(pontaClassica, "Ponta Completa");

    var carga = tinta.MontarEm(pontaCompleta, "Carga de Tinta");

    var cilindro = new Parte
    {
        Nome = "Cilindro",
        Propriedades = JsonConvert.SerializeObject( new { Cor = "Transparente" } )
    };

    var tampa = new Parte
    {
        Nome = "Tampa",
        Propriedades = JsonConvert.SerializeObject( new { Cor = "Azul", PodeRemover = true } )
    };

    var corpo = carga.MontarEm(cilindro, "Corpo Completo");

    var caneta = tampa.MontarEm(corpo, "Caneta Completa");

And in the end, it will have a structure like this:

  • Full Pen:
    • Cover: {"Color":"Blue","Powerver":true}
    • Full Body:
    • Tinta:
    • Ink: {"Colour":"Blue","Volume":2.0}
    • Ponta Completa:
      • Ponta Esferografica:
      • Sphere: {"Size":0.1,"Material":"Aluminium"}
      • Metal Tip: {"Material":"Brass","Weight":0.02}
      • Sphere: {"Size":0.1,"Material":"Aluminium"}
      • Ponta Classica: {"Material":"Plastic","Length":0.3}
      • Ponta Esferografica:
      • Sphere: {"Size":0.1,"Material":"Aluminium"}
      • Metal Tip: {"Material":"Brass","Weight":0.02}
      • Sphere: {"Size":0.1,"Material":"Aluminium"}
      • Ink: {"Colour":"Blue","Volume":2.0}
    • Cylinder: {"Colour":"Transparent"}
    • Tinta:
      • Ink: {"Colour":"Blue","Volume":2.0}
      • Ponta Completa:
      • Ponta Esferografica:
      • Sphere: {"Size":0.1,"Material":"Aluminium"}
      • Metal Tip: {"Material":"Brass","Weight":0.02}
      • Sphere: {"Size":0.1,"Material":"Aluminium"}
      • Ponta Classica: {"Material":"Plastic","Length":0.3}
      • Ponta Esferografica:
      • Sphere: {"Size":0.1,"Material":"Aluminium"}
      • Metal Tip: {"Material":"Brass","Weight":0.02}
        • Sphere: {"Size":0.1,"Material":"Aluminium"}
      • Ink: {"Colour":"Blue","Volume":2.0}
    • Cover: {"Color":"Blue","Powerver":true}

See working on Fiddle

Browser other questions tagged

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