Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC: How to work with entities with many child entities?
    primarykey
    data
    text
    <p>In the current examples on ASP.NET MVC I see quite basic entities, with simple CRUD methods.<br> But I'm not sure about what to do with more advanced models. Let me give an example:</p> <p>We have a garage website. The garage has:</p> <ul> <li>Inventory with <code>carparts</code></li> <li><code>Employees</code></li> <li><code>Customers</code></li> <li><code>Cars</code> that consists of all cars that are/were in the garage</li> </ul> <p>Now lets take the <code>car</code>, the car might have a collection of <code>employees</code> that worked on the car (derived from the original <code>employee</code> class, adds some extra props that tie him to the car), a collection of <code>carparts</code> that have been replaced (also derived, adds for example SerialNr, and <code>ReplacementDate</code> prop) , and of course a <code>customer</code> prop of the customer whoe owns the car.</p> <p>Now in <code>rest</code> I would like to see the following:</p> <pre><code>/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 </code></pre> <p>So how would I build my Controllers? Should I add all those CRUD methods for the child elements inside the <code>CarsController</code>? 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 <code>EmployeesInCar</code> controller (seems bad)... Thanks a lot.</p> <p><strong>EDIT:</strong><br> First of all the example is hypothetical only, just an example.<br> If I follow the suggestion I would have a <code>CarController</code> which only handles cars. And my PartsController would handle only Parts. But we have two sets of <code>Part</code>s, a general one (for the inventory); and we have a <code>Part</code> inside a car, which derives from the general one, but adds properties such as <code>SerialNumber</code> and <code>ReplacementDate</code>.<br> 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)</p> <pre><code>Index -&gt; Returns all GeneralParts in inventory. IndexByCar(int id) -&gt; Return all SpecificParts by Car. etc. etc. </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload