In the current examples on ASP.NET MVC I see quite basic entities, with simple CRUD methods.
But I'm not sure about what to do with more advanced models. Let me give an example:
We have a garage website. The garage has:
- Inventory with
carparts
Employees
Customers
Cars
that consists of all cars that are/were in the garage
Now lets take the car
, the car might have a collection of employees
that worked on the car (derived from the original employee
class, adds some extra props that tie him to the car), a collection of carparts
that have been replaced (also derived, adds for example SerialNr, and ReplacementDate
prop) , and of course a customer
prop of the customer whoe owns the car.
Now in rest
I would like to see the following:
/cars/423 [get] //show car # 423
/cars/423/edit [get] //shows the edit form (ajax enabled, so also shows the other props)
/cars/423/carparts [get] //gets the carparts inside the car
/cars/423/carparts/32/edit [post] //updates that specific carpart inside the specific car
/cars/423/employees [get] //gets the employees who worked on the car
/inventory [get]
/inventory/1234/edit [get] //gets the update form for carpart with ID 1234
/employees [get] //gets all the employees in the company
So how would I build my Controllers? Should I add all those CRUD methods for the child elements inside the CarsController
? Doesn't that make a very fat controller because of the tens of methods (this model is overly simplified, the car has many more parent-child relationships)? Or should I create a EmployeesInCar
controller (seems bad)...
Thanks a lot.
EDIT:
First of all the example is hypothetical only, just an example.
If I follow the suggestion I would have a CarController
which only handles cars. And my PartsController would handle only Parts. But we have two sets of Part
s, a general one (for the inventory); and we have a Part
inside a car, which derives from the general one, but adds properties such as SerialNumber
and ReplacementDate
.
So my Parts controller becomes quite fat, for the example let's call the entities: GeneralPart (in inventory) and SpecificPart (the derived class, with the extra properties)
Index -> Returns all GeneralParts in inventory.
IndexByCar(int id) -> Return all SpecificParts by Car.
etc. etc.
Because each action deals with either a General Part or a Specific Part. The same is for employees, we have the base employee class, and a specific one, with extra properties.