tags:

views:

502

answers:

2
A: 

Perhaps you haven't described the whole problem, but why not put the check in displayOutline() itself? perhaps route to a displayOutlineOrHelp() method, which looks essentially like

ModelAndView displayOutlineOrHelp(...)  {
    Unit unit = ... //code to get the unit the request refers to

    return unit.isAvailable() ? displayOutline(...) : displayHelp(...);   
}
Vinay Sajip
Thanks for your answer but its a bit of a miss. displayOutline() and displayHelp() are different methods with completely different views. An example url for either maybe app/unit/outline and /app/unit/help. The point is both require a unit which is in a status of 'ok'. Something like 20 of our view methods require a unit to be 'ok'. We could add a method call (method 1) but it seems more like a crosscutting concern (AOP - method 2) but it does not seem like the right solution. Ideally we are looking for a feature that is coherent with the standard oo metaphors without adding complexity.
Phil Gloyne
Ah - I see - I misunderstood the question. I would say that AOP is the way to do it - this is of course doable with pure OOP, but it needs more work that way.
Vinay Sajip
+1  A: 

Maybe I'm missing the point, but why should a user come to the controller if the Unit is withdrawn?

I would argue it is best to ensure that normally pages don't link to a controller that require the Unit to be 'OK', if that Unit is not 'OK'. If the state of the Unit changes between the time the referring page is rendered and the actual call comes in to the controller (it is not longer 'OK'), then the use of an exception to handle that event seems perfectly fine to me (like having an exception when an optimistic locking error occurs).

Fried Hoeben
Thanks Fried, I voted you up for your perspective but i cant give you the 'tick'. Basically I have come to the conclusion that stateful controllers are too hard. No-one seems to be using them in the real world. Instead devs opt for stateless controllers and store state in a httpsession object or similiar. Large companies seem to favor this approach which allows them to scale better. I would still love to hear others ideas on this problem however, Its called a number of things out there, the name i like most is throw-away controllers (because their lifecycle is bound to the request scope)
Phil Gloyne
FYI, We eventually went for the exception method in this case.
Phil Gloyne