From: Tim Peierls <[email protected]<mailto:[email protected]>>
Reply-To: discuss 
<[email protected]<mailto:[email protected]>>
Date: Mon, 8 Aug 2011 14:04:16 -0500
To: "[email protected]<mailto:[email protected]>" 
<[email protected]<mailto:[email protected]>>
Subject: Re: Spring inject a Form constructed using the requestEntity

On Mon, Aug 8, 2011 at 2:38 PM, Paul Morris 
<[email protected]<mailto:[email protected]>> wrote:
Unfortunately, in the case I mentioned, I am implementing an abstract method 
that takes a Representation as the argument and since Form doesn’t extend 
Representation I can't use it as the argument.

Well, you couldn't even if it did! :-)

Yeah (duh). I'm thinking covariant returns rather than arg type...

But I don't understand the point of such a generic abstract method if you're 
using the @Post annotation. What other callers are there for this method 
besides the internal Restlet machinery?
Here's why the interface was set to accept any Representation. Your feedback is 
welcome…

The interface is part of a very generic API for managing sessions and it's 
meant to support a wide variety of implementations, different session 
provisioners, etc.

public interface Session {
    @Post
    abstract Representation createSession(Representation entity);
    // Other methods.
}

So my implementation supports webform params as in:

username=pmorris&password=pmorrispmorris

as well as HTTP Basic Authentication (details in the Auth header, null entity 
body)

But some other developer implementing the interface may pass in some other 
Representation like JSON or XML, something that you wouldn't be consuming by 
casting to it a Form. Like:

{ "username" : "pmorris", "password" : "pmorrispmorris" }

With Representation he can pass in whatever he wants and work out the details 
in his concrete implementation.





I may just create the Form on the fly without Spring as in:

@Post
public Representation doPost(Representation entity) {
   Form form = new Form(entity);
   doSomethingWithForm(form);
   // etc.
}

I'm new to Spring and so I always seek to know how to construct an object via 
Spring rather than calling "new" in my code but I'm not sure how much value DI 
gives me in this case especially since it's just an object created locally, 
passed to another private method, and then trashed.

In this case, I don't think DI buys you anything -- "new Form(...)" is fine. 
But if you really want to:

public interface RepToFormConverter {
    Form convert(Representation rep);
}

public class SimpleRepToFormConverter implements RepToFormConverter {
    public Form convert(Representation rep) {
        return new Form(rep);
    }
}

Now you can inject a RepToFormConverter into your resources without exposing 
"new Form" to them. But this feels like massive over-kill.

--tim


This message and any included attachments are intended only for the addressee. 
The information contained in this message is confidential and may constitute 
proprietary or non-public information under international, federal, or state 
laws. Unauthorized forwarding, printing, copying, distribution, or use of such 
information is strictly prohibited and may be unlawful. If you are not the 
addressee, please promptly delete this message and notify the sender of the 
delivery error by e-mail.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2812635

Reply via email to