aspose file tools*
The moose likes Struts and the fly likes How much logic should go in Action class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How much logic should go in Action class?" Watch "How much logic should go in Action class?" New topic
Author

How much logic should go in Action class?

raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
HI I am working on a project which uses struts1.1. Most business logic is written into the Action class.I am not sure how much business logic should go in the Action class?Also is it good idea to invoke DAO directly in the Action class?

Thanks
Raj
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(The following is just an opinion--there's no "one right way" to do it.)

In general the only logic I put in action classes is logic directly related to the web portion of the application: things like what page to view next, and so on. All of my business logic is encapsulated in services. Services, in turn, are what use the DAOs.

This has at least a couple of benefits:

- Code that's "close together" physically (in the same files, methods, and so on) is all about one thing: the only code in an action is code relating to the web and the actions, the only code in services is code relating to business logic, and so on. It reduces the amount of things we have to think about when looking at any one chunk of code.

- It's easier to test the code: when all of the code is inside an action the only way to test it is to create a complete web request. When the code is separated it's possible to test the services independently of the web code, which is (almost) always much easier and faster. Also, when the services are injected into the actions, it's easy to create test services that return known data--perhaps even without using a real database, which can be slow--so that when we *do* test the web layer it's easier and potentially faster.
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
Hi David
Thanks for your reply.Its really helpful.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How much logic should go in Action class?
 
Similar Threads
A simple question
Topic: Struts and MVC. Question: how usually are working you?
Bad use of an ActionForm?
why business logic not written in Action class
in Action I should not write business logic ! RIGHT ?