Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How much logic should go in Action class?

 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David
Thanks for your reply.Its really helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic