I have abstract class "Vehicle" an two child classes "Car" and "Motorcycle". I am trying to build a MVC controller that can save/edit/update/delete both "Car" and "Motorcycle". My controller needs to inherit from a Abstract BaseController that takes TEntity as parameter. My problem is that i don't know how i can achieve this when i don't know what type of object i want to save. My first plan was to pass "Vehicle" to the BaseController but that doesn't work because "Vehicle" does not have a public constructor. //A junior developer
1 Answer
I think I understand what you mean. You could try and use Generic Interfaces?
public interface IVehicle<T> where T : Entity
{
void Save(T entity);
T Get(int id);
}
etc.
You can then use the interface.
public IVehicle<Car> Car { get; set; }
//... Stuff
var result = Car.Get(1);
Car.Save(entity);
ApiControllerSave()+Delete()etc. Include what you've done so far and we can work out a plan from there.