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.
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.