A Spring Controller Invoker Service w/ Groovy Named Params

I’m attempting to use the invoker pattern with a spring controller. For instance…
/app/invoke/hamburger/makeme
Navigating to this URL with either a GET or a POST should do the following…

  1. 1. Look up and get a service called ‘HamburgerService’ from the spring context.
  2. 2. Invoke a method on our service bean called ‘makeme’
  3. 3. If any params are passed in, pass them in as arguments to the makeme method.
Example Groovy code:
HamburgerService.groovy
class HamburgerService {
def makeme(Boolean withCheese, String sauce) {
// make delicious samy
}
}
ServiceInvoker.groovy
@Controller
class ServiceInvoker {
@RequestMapping(value = “/invoke/{service}/{method}”)
def handleServiceInvoker(@PathVariable String service, @PathVariable String method, WebRequest request) {
def bean = Lookup.get(service)
bean.“$method”(request.getParameterMap())
}
}

The problem is… And by the way the above code is not complete or functional by any means… How do we take a potentially unordered list of parameters passed in to our Controller and reflect on the service class and pass in an ordered list of values…

The solution? I don’t really know… Seems the best solution is to avoid the ordered list of the Service method. This makes things a bit hairy from a java prospective, but actually works out quite well from a groovy prospective.

Now, instead of calling (a basic call here…)

hamburgerService.makeme(true, “spicy mayo”)
We can (have to)…

hamburgerService.makeme(withCheese: true, source: “spicy mayo”)
And our method signature changes to…

def makeme(Map args) { }
or

def makeme = { Boolean withCheese, String sauce ->
}

or

def makeme = {
  it.withCheese
  it.sauce
}

Perhaps this is the best way and maybe this is how programming should be done. If you think about it, arguments of a method signature are arbitrary and should be expected unordered. Why do we care how things are passed in, just that they are passed in.

But, this does break our ‘Strong’ method signature. Perhaps our compilers should be reflecting on the doc in the comments and based on that determine what the method wants (hey it would force us to have good docs)…

Hmm…



Published by and tagged Code using 332 words.