Friday, June 01, 2007

Action based frameworks and fine-grained component dependencies

Working in an action-based framework, such as Struts, there's the inevitable problem where you may want to render a page which has content that the Action hasn't prepared yet.

The prime example is in the case of bad validation. Let's say we're viewing a page which has dynamic content on it, such as a list of products in a store. If I submit a form on that page with a validation error, in default cases the execution code, which probably retrieved that list of products, doesn't fire, and I'm forwarded to a validation error result page. Same action, different result page, but I still probably want to have the list of products on the page. I just don't want to process the form

The problem is that action based frameworks make an action do many things. It's not just a form processor, or just a product retriever, or a User retriever. It's all of those things at the same time.

There are solutions out there, and this is mostly a brainstorm about which kinds work well, and which do not.

In Struts 2, there's a tag which can call another action. It's kind of like action chaining. For example, if I have an action named "productList" which retrieves a set of products, I can just insert that tag, and I'm good to go. The nice thing about this is that it allows for more fine grained actions, tailored to specific tasks. On the down side, more than one action is being fired per page view, which can be pretty expensive.

There's also the option of creating a custom jsp tag. This too can be pretty cool. Total customization is possible, and at a lower-level. On the down side, Custom tags can get a little out of hand, if they're made to do more than intended. It's also pretty far outside of the realm of the action framework, so they might not play well together.

JSF: I haven't done anything with JSF, but as I understand it, they'd work similar to the custom tags. Same down side, I think as well

Dependency injection: Another place I haven't roamed quite yet. Although, I'm sooooo on the brink of doing so. Allowing an action to be populated ala carte could be very beneficial, however, there's probably a little plumbing to do, on a per-action basis, to make those dependencies available to a page. Writing getters and setters for every action which *might* want to use a list of products could be a pain.

The answer? I don't know!

I'm pretty actively working in a Struts2 environment, so, I'm thinking that using the struts action tag might be the way to go. However, I don't know how I feel about tying an action to a specific task.

No comments: