'Spliting a big table by category into smaller ones
I have an ASP.NET Core Web API that uses EF Core. I have multiple seller types (doctor / styler), all having the same model i.e. id, name, location.
I was thinking that instead of pushing every seller into a seller table and add a category column to categories them, to have a base model class that each seller is going to inherit from, and have each seller type in his table (for performance reasons on large databases).
Ex: if I want a doctor I query only the doctor table not the whole seller table.
Now my problem is that I want to do this from a SellerController that receives only Seller objects and inside it I would like to cast them into doctor/styler models to use the right table.
But how can I do this?
Like beside a giant switch case in each endpoint of my controller. I thought that I could build a convertor that receives a Seller object and returns a doctor/styler object but what should that return type be then?
Old
public class Stylist
{
public int Id { get; set; }
public string Name { get; set; }
public int Location { get; set; }
}
public class Doctor
{
public int Id { get; set; }
public string Name { get; set; }
public int Location { get; set; }
}
New
public class BaseSeller
{
public int Id { get; set; }
public string Name { get; set; }
public int Location { get; set; }
}
public class Stylist : BaseSeller
{
}
public class Stylist : BaseSeller
{
}
Solution 1:[1]
Maybe you can use this,pass BaseSeller and the ClassName which you want to convert to:
public Object ConvertBaseSeller(BaseSeller Seller,string ClassName) {
if (ClassName == "Stylist")
{
Stylist s = JsonConvert.DeserializeObject<Stylist>(JsonConvert.SerializeObject(Seller));
return s;
}
else{
Doctor d= JsonConvert.DeserializeObject<Doctor>(JsonConvert.SerializeObject(Seller));
return d;
}
}
and then call it like this:
var s = ConvertBaseSeller(new BaseSeller(), "Doctor");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yiyi You |
