Building a handler chain using registry

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Building a handler chain using registry

Rob Fletcher
I have some common logic for a couple of my handler classes that I'm trying to refactor out. Basically they need to lookup a 'user' object and pass it on to the next handler if found or return 404 if not.

I've ended up by registering handlers like this:

    handler("api/:username/messages", new RequiresUserHandler(registry.get(UserStore), registry.get(MessageHandler)))

Since I'm using `RequiresUserHandler` in a couple of places and it needs a different handler passed to its 2nd constructor parameter each time it seems like I can't register it with Guice. However this looks a bit of a mess as the 1st constructor argument is always the same and I can't help thinking there must be a better way to do it.
Reply | Threaded
Open this post in threaded view
|

Re: Building a handler chain using registry

Luke Daley
Administrator
You could implement some kind of parameterised factory, or do it inline as a closure var.
Reply | Threaded
Open this post in threaded view
|

Re: Building a handler chain using registry

Luke Daley
Administrator
In reply to this post by Rob Fletcher
Is this a case for @Assisted?

A lighter weight alternative would be to create a local closure var that acts as a factory.