Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Request object, to pass or not to pass?  RSS feed

 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I create objects that I call AjaxHandlers for many of my applications. They are called from a servlet and they return a JSONObject. It's been my habit to pass the HttpServletRequest object to this class and let it extract the request parameters. Is that a bad idea? Would it be better to let the servlet extract the parameters and pass them as primitive data types or a Map to the AjaxHandler? The more I consider this, the more I think maybe the request object should stay in the servlet and not be passed around but I'm not sure if it really makes a difference.
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I would do. The difference for me would be testability and decoupling. By removing the dependency on HttpServletRequest, the tests would probably be easier to write.
...and they return a JSONObject

I would also reconsider the name "AjaxHandler" since AJAX is an implementation detail (one way input can come in to the application) and that will probably become apparent in the tests ("Where is AJAX in all this?") -- if anything, these are JSON producers.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why exactly do you think it's a bad idea?

By passing HttpServletRequest objects to your AjaxHandlers, you make your AjaxHandlers depend on the servlet API (since HttpServletRequest is from the servlet API). But is that really a problem? (Are you planning to someday use these AjaxHandlers in a context where it would be inappropriate for them to depend on the servlet API?).

I can imagine that it might make testing your AjaxHandlers with unit tests a little bit harder, because you'd want to pass them mock HttpServletRequest objects. On the other hand, HttpServletRequest is an interface and with a mocking framework such as Mockito it would be easy to create mock HttpServletRequest objects.
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Why exactly do you think it's a bad idea?

... you make your AjaxHandlers depend on the servlet API (since HttpServletRequest is from the servlet API). But is that really a problem? (Are you planning to someday use these AjaxHandlers in a context where it would be inappropriate for them to depend on the servlet API?).

It takes a while to recognize this but the name "AjaxHandler" influences the design choices. If the name were JSONProducer instead, it's more apparent that there's no need to be dependent on the HttpServletRequest API since the source of input should not be a concern of this class. With the name "JSONProducer" you see that its responsibility is to produce JSON, not to extract input from an HttpServletRequest. The name "AjaxHandler" misleads you into thinking that AJAX is important to this class.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that seems to be one for and one against. I questioned my own design because it seemed to be an example of high coupling, but as you say, will they ever really be used without a servlet? Probably not. At least I can't imagine how.

On the other hand, the testability seems to be a good argument for passing the parameters instead of the request object. I think I'm leaning in that direction.

As for the naming, that's a good point. I used Ajax in the name simply because I invoke them with an Ajax call in the Javascript, but maybe JSONGenerator or similar would be a better, more descriptive name.

Thanks for the feedback.

[edit] I like JSONProducer. I'll be stealing that one.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strongly against. In my opinion, the servlet resources should never be passed outside the servlet. If you need the parameters, pass them, not the request, to any other classes or methods,
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:So that seems to be one for and one against. I questioned my own design because it seemed to be an example of high coupling, but as you say, will they ever really be used without a servlet? Probably not. At least I can't imagine how.
...
[edit] I like JSONProducer. I'll be stealing that one.

I consider my tests as valuable clients that show me a lot of things about my design so even if my production code won't ever get input any other way besides AJAX, my tests will make me account for an alternative way for input to be provided.

If you're going to steal it, you might want to use JsonProducer so it's prettier
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "against" votes have it. I'm rewriting my latest one now and I like it better already.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!