This post originated from an RSS feed registered with Java Buzz
by Russell Beattie.
Original Post: XAOism in the Real World
Feed Title: Russell Beattie Notebook
Feed URL: http://www.russellbeattie.com/notebook/rss.jsp?q=java,code,mobile
Feed Description: My online notebook with thoughts, comments, links and more.
Okay, so I over-architected. I admit it. I've been spending a ridiculous amount of time working on my new server-side system. I was trying to work out an MVC framework that included XML at its core for data and persistence, while allowing for arbitrary URLs and multiple views based on browser and device type with JSTL as the primary page logic. I finally at the end ditched Struts and wrote my own controller/action based system to make me really happy and got all the pieces in place. After literally months of fucking around with all this stuff, I finally had what I wanted and was ready to get to work.
And that's when I realized that it was way *too* architected! In the system as designed I would need 3 to 4 files (java, jsp, etc.) per request type, and that there were probably 40 different requests that I could make in my first web app. This meant managing 120+ files do the most basic website! AHHHHHH! WTF was I thinking? The reason for this is mostly because of my XML Access Objects that I put at the back to grab data. The idea (for those of you reading this for the first time) is that the XAO objects provide a layer - sort of like a DAO - between your data and your web app, but always formats the returned data as XML.
I had a hierarchy of Interfaces and XAOs for various tables and queries and business objects, and caching layers, and DB helper classes... It was *beautiful*. If I was in charge of a dispersed 30 person development team, this is exactly the way I'd do this. It's a clean and standard separation of layers, yet performant (due to cacheing at both the data and presentation layers) and completely server-agnostic thanks to XML. Neato!
Except that it's just me doing development isn't it? After getting really depressed for a few days and seriously considering scrapping it all and just going back to basic .JSP pages written with the JSTL SQL tags and a big Servlet file ("Just like PHP!", I thought), I finally chatted with Diego a bit about what I was doing and realized that the complexity that was bothering me was all this XAO shit that I needed to *wack* it.
So I did. First I decided to cut right back to the bone. All the XAO stuff was gone. And all the Actions did their own queries to the db and formatted as XML for the JSTL XML tags to use on the pages. Then, after I looked at it from this new perspective, I was able to extract the common functionality out into a new, more basic type of XAO class that I simply called DatabaseXAO. This class has a lot less rules than my original XAOs did and functions more or less like a normal DAO that returns XML Strings instead of Lists and HashMaps of beans. It's a singleton so that I can share a LRUCache object among the query methods, and has basic methods like getLatestEntries(int limit) and getSearchResults(String search, int limit). These methods use a shared database function that will format the query as XML without much work and the results are cached for quicker retrieval later. DatabaseXAO also has methods to monitor the cache size and clear it if necessary.
Later I'll break it out into more specific XAOs like you would for normal DAOs. Even though this seems obvious to do, I was looking for a more generic/global way of doing things (inspired by Cocoon), but realized at the end that the simplest form is much faster for development. This had the side benefit of significantly cleaning up my Actions as well. Now because of the shared query object, I can combine several different types of requests into one Action and return the results based on a method to the DatabaseXAO. Much more manageable.
Okay, so if none of this made sense, don't worry. The take away is this: With proper cacheing of XML, you can use XML as the transport between your WebApp and your data by using XAOs. XAOs are simply objects that return valid XML derived from arbitrary data sources (dbs, files, filesystems, etc.). Your WebApp only needs to know how to work with XML and your data can come from anywhere. Switch DBs and all you need to change is your XAOs.
Okay, that's it for today's head-smack "duh" observation. Back to work.