Answer the question
In order to leave comments, you need to log in
CodeFirst, how to get inheritables from a base object?
The title is a bit vague because I don't know how to put it concisely.
The essence of the problem:
Let's say we have an object - a product. The product has basic characteristics (name, description, image path, etc.). Goods can be of different types, in my case it is simple, complex and composite. I have this implemented by inheritance i.e. There is a basic product object and other types of products are inherited from it. Entity Fraimwork placed all products in one table and created an additional Discriminator field in it, which stores the name of the object type.
Now the most interesting thing is that for each type of product the cost is calculated differently (each type has its own logic), how can I implement the logic when, for example, I receive all products, and now, depending on their type, I need to calculate their cost?
Object examples:
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
}
public class SimpleProduct:Product
{
public new int GetShopPrice(City city)
{
//логика
}
}
public class ModuleProduct:Product
{
public new int GetShopPrice(City city)
{
//логика
}
}
Answer the question
In order to leave comments, you need to log in
The GetShopPrice method is made virtual on the Product and is overridden on the descendants.
Each entity will call its own method.
What is the problem?
public abstract class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public abstract int GetShopPrice(City city);
}
public sealed class SimpleProduct : Product
{
public override int GetShopPrice(City city)
{
//логика
}
}
public sealed class ModuleProduct : Product
{
public override int GetShopPrice(City city)
{
//логика
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question