20040219 Thursday February 19, 2004

Simplify your AppFuse Managers I figured out a way to remove any ActionForm references from my Managers yesterday. It was pretty simple, but I thought others might benefit from this knowledge. Basically, you just need to add a couple methods to your BaseManager.java file. Here is the latest one from CVS HEAD. Then you can pretty much eliminate any *Form references in your Manager by using the convert(Object) method. This method will convert POJOs <-> Forms and vise versa. You will likely have to do some tweaks on the package names if you're using anything < v1.4. Looking at this diff of the UserManager before and after this change will show you what's changed.

I started this task thinking I could remove the need for Forms in my classpath when compiling the service layer. However, I soon discovered that my Tests have Forms all over the place - since they replicate what my Actions will pass in. Oh well, at least my code is a bit cleaner now.

Of course, this code will become even simpler when I start using an MVC Framework that allows my POJOs to be my data handlers on the UI. These frameworks make me question if I even need Managers. Then again, it's nice to unit test what your DAOs are returning/receiving from the web layer. Posted in Java at Feb 19 2004, 11:28:41 AM MST 2 Comments

Comments:

You are using Struts, why would you not be able to put your POJO's in your ActionForms? If there is a limitation, then I would think it would be due to Hibernate or the persistence layer of choice. I have never really understood the need for conversion when you can easily nest your POJO's into your ActionForms. Is it a package dependency that you are trying to avoid? Seperation of layers? I must be missing something, please elaborate.

Posted by Carl Fyffe on February 19, 2004 at 10:28 PM MST #

My reasons are simple - I generate my Forms from POJOs and all my properties are Strings. The main reason for this is so I can re-display values in the form elements when they don't validate properly. I could certainly nest my POJOs in my forms (and therefore have very simple forms). I used to do it this way and it felt ugly, but it certainly was easy. Here's an idea that I hope to bring to reality in the next year or so:

1. Override ActionServlet so that the execute method takes an Object instead of an ActionForm.
2. Allow POJOs to be used in Struts Actions and use the Validator and possibly AOP or Interceptors to validate POJOs.
3. Continue to use the Validator's client-side validation feature.
4. Figure out a way to accomplish the above, but still allow for displaying any invalid input in form elements.

If the Validator could validate POJOs as easily as it can ActionForms, it could be used in any MVC framework - powerful stuff!

Posted by Matt Raible on February 23, 2004 at 11:31 PM MST #

Post a Comment:
  • HTML Syntax: Allowed
Click me to subscribe
Matt Raible is a Web Architecture Consultant specializing in open source frameworks.
« May 2012
SunMonTueWedThuFriSat
  
1
2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
23
24
25
26
27
28
29
30
31
  
       
Today

Recent Entries

Tag Cloud