PDA

View Full Version : Current HttpServletRequest as a dependency (to inject).


tellary
Mar 27th, 2007, 08:12 AM
I want to make a current HttpServletRequest to be a bean in request scope in servlet environment to inject it as a dependency to other services.
In my humble opinion, this let reach a great degree of loose coupling. There exists a number of interfaces which do not have a HttpServletRequest as parameter in their methods, but their implementation needs "contextual" information from current request. An example of such interface is Validator. Certainly, I can put this contextual information into commandObject, in case of Validator. But I don't think this is elegant way to solve a problem. Let imagine a http-service which recieves all http-requests with 'customerId' parameter. This service has some validation logic based on 'current customerId' information. I want this validator to have an implementation of CustomerIdResolver interface as dependency.

interface CustomerIdResolver {
String getCustomerId();
}

Using interface for resolving customer gives bonuses in following situations:

1. One ("Main") implementation of this interface uses request-scoped HttpServletRequest described earlier to resolve customer. If I need to implement and validate similar form for my most 'valuable' customer, instead of current, I would implement another one CustomerIdResoulver which would use some StatisticManager to return customerId of most 'valuable' customer. There could exist more implementations. And changing a behaviour of same validator would be a question of configuration only.

2. One more plus for using separate interface for resolving customer instead of putting its ID into commandObject would become apparent if I mention that I can use such validator not only in servlet environment. In the latest case I can create another one implementation to resolve customerId in new environment.

Such an aproach seems very straitforward to me, but I'm afraid that I'm missing some "invisible" pitfalls here.
I wish some experts would left their opinions about described approach.

With best regards,
Silvestrov Ilya.

Marten Deinum
Mar 27th, 2007, 10:02 AM
Why not just have your commandObject implement your CustomerIdResolver interface? Wouldn't that handle your case?

pmularien
Mar 27th, 2007, 10:42 AM
Actually, I'm sorry, I can't think of a use case where this would be desirable at all. Getting the ServletRequest involved at a lower level in your application stack than the Controller seems like a clear violation of the principle of "separation of concerns". Maybe if you can articulate a clear example of where you'd need to access the servlet request outside of the controller, it would help us guide you. The code you posted doesn't involve the servlet request at all, so it's hard to follow why you need it :)

tellary
Mar 27th, 2007, 12:23 PM
Ok, thank you for reply. I'll post an example code here next morning, because I'm very interested in somebody to review it.

2mdeinum:
That doesn't matter of "who" (commandObject or any other bean) would implement CustomerIdResolver the question is how this implementation would be provided with current HttpServletRequest. Anyway, example would be here tomorrow.