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?
– Damon Dudek
I fixed a line in the code, you had inherited it
ProdutoTipoB
in classProdutoTipoA
, I switched toProdutoBase
, I don’t know if it’s correct, if I’m not canceling the edition.– Leonardo Bonetti
she wouldn’t inherit from the productBase
– Dorathoto
@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
– Dorathoto
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?
– Leandro Simões
@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
– Dorathoto
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.
– Gustavo S. Jardim
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?
– Dorathoto
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
– Gustavo S. Jardim