Modeling - Model

2

I'm having trouble creating my models.

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

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

However, the Type A products (% w / o%) can be disassembled and thus create 4 new pieces, in which I also want to register. (until then I'm going to register a model called ProdutosTipoAPartes) I thought about creating another Model where I could store the Id of the base product

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 would I have some kind of virtual relationship with the other types TipoProduto==A , A , B or C ?

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 ProductsA, ProductsB inherit base?

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

And to complete, 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 do not 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 it were child products.
    • Create 4 separate products and turn my life into a madness ..
asked by anonymous 24.08.2017 / 21:16

1 answer

3

If you had given a more concrete example, it would help you better. I think you're working with structures .

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

publicclassParte{publicGuidId{get;set;}publicstringNome{get;set;}publicstringPropriedades{get;set;}publicList<Parte>Partes{get;set;}publicParteMontaEm{get;set;}publicParte(){Id=Guid.NewGuid();Partes=newList<Parte>();}publicParteMontarEm(Parteparte,stringnome){MontaEm=parte;parte.Partes.Add(this);varnovaParte=newParte{Nome=nome};novaParte.Partes.AddRange(new[]{this,parte});returnnovaParte;}}

Wheneveryoumountoneparttoanother,anewpartiscreated.Thisisnotsimpletocontrol,andyoucanaddmanyotherfeatures,suchaswhichpartcanconnecttowhichpart,whetheritcanberemovable,whetheritcanbeinterchangeable,etc.

varesfera=newParte{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");

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

  
  • Full Pen:      
    • Cover: {"Color": "Blue", "CanRemove": true}
    •   
    • Full Body:
    •   
    • Ink Load:
    •   
    • Ink: {"Color": "Blue", "Volume": 2.0}
    •   
    • Full Tip:      
      • Ball Point:
      •   
      • Sphere: {"Size": 0.1, "Material": "Aluminum"}
      •   
      • Metal Tip: {"Material": "Brass", "Weight": 0.02}
      •   
      • Sphere: {"Size": 0.1, "Material": "Aluminum"}
      •   
      • Classic Tip: {"Material": "Plastic", "Length": 0.3}
      •   
      • Ball Point:
      •   
      • Sphere: {"Size": 0.1, "Material": "Aluminum"}
      •   
      • Metal Tip: {"Material": "Brass", "Weight": 0.02}
      •   
      • Sphere: {"Size": 0.1, "Material": "Aluminum"}
      •   
      • Ink: {"Color": "Blue", "Volume": 2.0}
      •   
    •   
    • Cylinder: {"Color": "Transparent"}
    •   
    • Ink Charge:      
      • Ink: {"Color": "Blue", "Volume": 2.0}
      •   
      • Full Tip:
      •   
      • Ball Point:
      •   
      • Sphere: {"Size": 0.1, "Material": "Aluminum"}
      •   
      • Metal Tip: {"Material": "Brass", "Weight": 0.02}
      •   
      • Sphere: {"Size": 0.1, "Material": "Aluminum"}
      •   
      • Classic Tip: {"Material": "Plastic", "Length": 0.3}
      •   
      • Ball Point:
      •   
      • Sphere: {"Size": 0.1, "Material": "Aluminum"}
      •   
      • Metal Tip: {"Material": "Brass", "Weight": 0.02}      
        • Sphere: {"Size": 0.1, "Material": "Aluminum"}
        •   
      •   
      • Ink: {"Color": "Blue", "Volume": 2.0}
      •   
    •   
    • Cover: {"Color": "Blue", "CanRemove": true}
    •   
  •   

See it working on Fiddle

    
25.08.2017 / 10:26