I've recently started working on my first
Struts application and I seem to run into a similar problem fairly often.
A given Action class is responsible for gathering information from the framework (form bean/session/etc.) and formatting that in a way so that the model can process the request
without knowing it is part of a web application. Perhaps my concept of what an Action class should be doing is incorrect but that, in my mind, is one of the tasks it should perform.
The Model then processes that request and, optionally, returns data to the Action class. It does all of this with no knowledge that it is part of a web application. That, of course, increases the chance that your business components can be re-used in another application.
My trouble really seems to be with the interaction of these two layers. Often, I find that my Action class needs to pull multiple fields from the Form Bean and pass them to the model. In cases in which I need to pass a large number of parameters to the model, it might be easier to pass a single object, rather than a whole slew of individual parameters.
Similarly, when the model has completed processing the request, it will need to return some data to the Action class. If there is more than one piece of data being returned (which seems to almost always be the case), you'll want to return an object (either via return or In/Out parameter) to the Action class.
One solution I've seen used around the office is to simply pass the entire form bean to the model. The model can grab the data it needs from the form bean and then populate additional fields in the form bean with the data it needs to return. While this is easy, it seems like it violates some basic principles. Namely, it makes your business logic less reusable in a couple ways. First of all, it now contains web application information, making it more difficult to use outside of a web application (i.e. a Desktop GUI app). Secondly, your business logic is now, more or less, dependent upon Struts. If you decide to go with another framework (a home-grown one, perhaps), your business logic would have to change.
Based on that, I'd really rather not pass the form bean to my model layer. I'd rather take the extra effort in my Action class to pull the data from the form bean and give it to the model and take the return data from the model and put it in my form bean. The problem I have with this is that we now need brand new objects to pass to the model and get back from the model. These objects are probably either identical (or awfulyl similar) to the form bean. It just seems silly to me to have multiple classes that are so similar in an application. If nothing else, it increases confusion as far as what each class is for.
So, is there a good approach to separating your business logic from your web application? It's something I've been struggling with for some time now and I just never seem to able to nail down a real nice solution.
Thanks.