Hello Michael,
In my own opinion, I would put the logic into the Resource class, either
in a static method, or in an instance method.
These methods just concat data from the request, maybe the response, and
one domain object (or more if "bar" is a sub-element of "foo" for example).
I must say that you can get the base of the Url from the request object,
as it allows you to have access to the host reference with the attribute
"hostRef".
Best regards,
Thierry Boileau
Hi all-
This may really be more of an OOP question rather than a Restlet
question, but I'm curious as what people are using as a general strategy
for mapping resources to domain objects. Specifically in cases where
there are properties of the resource that that are not part of the
domain object.
Consider these domain objects:
class Foo {
int id;
String description;
List<Bar> Bars;
}
class Bar {
int id;
String description;
}
Then I create a class FooResource that when represented looks like this:
<foo id="1" href="http://example.com/foos/1>
<description>this is foo 1</description>
<bars>
<bar id="5" href="http://example.com/bars/5" />
<bar id="8" href="http://example.com/bars/8" />
<bar id="21" href="http://example.com/bars/21" />
</bars>
</foo>
Currently FooResource and the XmlRreprentation (a subclass of
Representation) that is returned by getRepresentation(variant) both have
an instance of Foo as a backing object.
My question is where do people generally put the logic for determining
something like the value of the href attribute? Having it as a property
of class Foo is a bad idea because Foos in the context of the domain
don't have hrefs, only FooResources do. Putting the logic in the
representation class isn't ideal because I'd like them to be fairly dumb
and just take an instance of a class and be able to represent it knowing
only it's type (in the case of an xml representation we're using Betwixt
to go from object->xml).
It seems to me the best place for this logic would be in FooResource but
have it applied in the resource class (i.e, passing it some sort of
strategy). But I'm having a trouble coming up with a (semi-)elegant way
to encapsulate this logic without having lots of special cases. One more
promising idea is to pass the representation a regex pattern it will use
to post-process the representation. The other one is to use cglib to add
properties/methods to the backing object at runtime.
Anyone else doing something similar or have any ideas/opinions? (even if
it's "you're doing everything wrong" :)
Cheers,
Michael