title: RESTful design: when to use sub-resources?
body: <p>When designing resource hierarchies, when should one use sub-resources?</p>
  
  <p>I used to believe that when a resource could not exist without another, it should be represented as its sub-resource. I recently ran across this counter-example:</p>
  
  <ul>
  <li>An employee is uniquely identifiable across all companies.</li>
  <li>An employee's access control and life-cycle depend on the company.</li>
  </ul>
  
  <p>I modeled this as: <code>/companies/{companyName}/employee/{employeeId}</code></p>
  
  <p>Notice, I don't need to look up the company in order to locate the employee, so should I? If I do, I'm paying a price to look up information I don't need. If I don't, this URL mistakenly returns HTTP 200:</p>
  
  <p><code>/companies/{nonExistingName}/employee/{existingId}</code></p>
  
  <ol>
  <li>How should I represent the fact that a resource to <em>belongs</em> to another?</li>
  <li>How should I represent the fact that a resource <em>cannot be identified</em> without another?</li>
  <li>What relationships are sub-resources meant and not meant to model?</li>
  </ol>