1

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

2
  • show your example code pls. But i think if you trying to create CRUD you should look at ApiController Commented Oct 12, 2015 at 8:34
  • You're best off using interfaces and passing these around rather than abstract types. The interface will be the contract your objects will conform to so in your case it will be things like Save() + Delete() etc. Include what you've done so far and we can work out a plan from there. Commented Oct 12, 2015 at 8:35

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.