Reputation: 951
I have a design of a person having a collection of subjects like Person ---> Subject (1 to many)
If I access a person with an Id of 1 (http://localhost/person/1
),
it will show all the person's details and the collection details.
How do I design a URL for just accessing the subjects for the person?
Should it be
http://localhost/person/1/subjects
?
Can I post to that URL to add subjects?
Can I put to that URL to update the subjects?
What handler should handle subjects of a person? Should it be a subject handler with Get and a personid parameter or a person handler returning a collection of subjects with a return method of subjects?
Upvotes: 1
Views: 137
Reputation: 6778
In the above case, if you need to use the CreateUri extension method to create hypermedia links on your objects, you would need to add parent Ids to the children objects.
using System;
using System.Collections.Generic;
using OpenRasta.Codecs.Razor.TestApp.Codecs;
using OpenRasta.Configuration;
using OpenRasta.Web;
namespace OpenRasta.Codecs.Razor.TestApp
{
public class Configuration : IConfigurationSource //, IDependencyResolverFactory
{
public void Configure()
{
using (OpenRastaConfiguration.Manual)
{
ResourceSpace.Has.ResourcesOfType<Person>()
.AtUri("/person/{personId}")
.HandledBy<PersonHandler>()
.AsXmlDataContract();
ResourceSpace.Has.ResourcesOfType<Subject>()
.AtUri("/person/{personId}/subjects/{subjectId}")
.HandledBy<SubjectHandler>()
.AsXmlDataContract();
}
}
}
public class SubjectHandler
{
public Subject Get(int personId, int subjectId)
{
var subject = new Subject()
{Name = string.Format("Person {0} Subject {1}", personId, subjectId), SubjectId = subjectId}; //PersonId = personId
Uri link = subject.CreateUri(); //this line will fail unless Subject has personId property.
subject.Link = link.AbsoluteUri;
return subject;
}
}
public class Subject
{
//public int PersonId {get; set;} //Need to add Parent link if using CreateUri() for OR to create link
public int SubjectId { get; set; }
public string Name { get; set; }
public string Link { get; set; }
}
public class PersonHandler
{
public Person Get(int personId)
{
return new Person() {Name = "Test" + personId, PersonId = personId};
}
}
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
}
}
Upvotes: 0
Reputation: 6766
From a ReST perspective it doesn't matter in the slightest, URIs are just opaque identifiers so how they're structured is an orthogonal concern.
From an OR point of view, you can just register AtUri("/person/{id}/subjects/{subjectid}") and have your method with a signature of Post(int id, int subjectid), that will work jut fine.
Now from a design perspective, anything that is to be accessed independently is another resource, so the collection of subjects and each subject are independent resources. OR makes a lot of assumptions relying on you mapping each indepednent resource independently, otherwise things like URI creation break quickly.
Upvotes: 1