Tuesday, July 21, 2009

DAOs ... not... dead? <shotgun blast>

Recently, some guy wrote a whole lot of blog entries about J2EE design patterns he and his team came up with.

As a whole the entries are very nice, packed with info and so on. One particular blog entry struck a cord with me and I wanted to discuss it. It is the DAO object entry.

The beginning of that post immediately addresses the actual need for DAOs in an architecture, by referring to another blog post. The conclusion is that "it depends." If your application is complex, you should use DAOs, as they provide a thin layer on top of JPA and its Entity Manager. In this post I argue that encapsulating the EntityManager functionality is not a job for the DAO layer, and that it in fact needs to be rethought, and eventually absorbed by the Service Facade layer.

There are two concerns that are addressed by the DAO pattern. One is actual persistence, and the other is a logical layer within the architecture of the application. As with all design elements there is a downward concern, as well as an upward concern. The downward concern is the persistence. How will we store the data? The upward concern is the way this pattern will fit into our architecture.

I am not going to use the straw man "there are too many layers" argument which dismisses this debate. The job of the layers is to make an application extensible to future business needs, and goes hand in hand with the SOA concept that businesses usually salivate over. So, let us assume either a medium size application or an application that will see rapid growth in functionality in the near future.

I will discuss the downward concern first. DAOs provide an abstraction of the specifics of persistence. DAOs need to map object data to database tables and back to objects. The JPA standard provides a specification for doing exactly this. There are many JPA providers, as indicated by Vincent Partington's blog. At the end of the day, most of these competitors will use JDBC to write some data to a database. They will also need to figure out how to map the data from JDBC results sets to Java objects and back. As you can see the JPA Entity Manager takes care of the downward concern completely.

The upward concern is much more interesting. DAOs offer many advantages to the client programmer. One big benefit is type safety. Another, perhaps bigger benefit is the separation of concerns, i.e. the creation of the "DAO layer." Why even have layers? Partington accurately portrays the flexibility that is gained when a layer is separate within the architecture. Not only can its functionality be changed without affecting the rest of the codebase (he cites logging as an example) but additionally, the functionality contained within can be reused to build out other components which need it, higher in the hierarchy. There are also other, minor benefits such as the conglomeration of all persistence code in one package, and so on. JPA definitely does not address the typesafety concern, and it lacks the cohesiveness to be used as a layer in our hypothetical mid-sized application.

So a DAO-less architecture using JPA seems to be 1 for 2 here. Or is it? Another one of the posts discusses the Service Facade and DTOs. Yet another well thought out entry, it correctly provides the justifications for DTOs (sometimes called Light Beans) and Service Facades. However there is one concern which I want to add, regarding the Service Facade.

One big time sink in real world projects is communication between developers and business analysts. Even intelligent, motivated and hardworking coworkers have a hard time getting the details knocked out when apps are developed. One of the core challenges they face is language. As human beings we cannot help but use overloaded words and phrases that mean something to us, but not quite the same thing to someone else. A UserEntity might mean something to a developer, but it might mean something completely different to a business analyst. The chief virtue of Object Oriented design is our ability to model code on real world problems and their solutions. Therefore it is my claim that service facade objects and their methods must be identified as business objects and verbs by the business analyst first, and then created verbatim by the architect of the application in the Service Facade layer.

Business logic has no place in the persistence layer. Methods called things like "findExecutingChangePlans" should not be present there at all. Unfortuantely, the old DAO pattern, as presented by Partington, does wed those two concerns: the upward and the downward.

The service facade classes implement two interfaces, the proposed IDataAccess<K,V> and also a particular IBusinessInterface. They do this by extending a AbstractBaseDataAccess<K,V> class, which implements all of the "workhorse" methods as proposed by Partington. But they also implement the various "business verbs" created within the IBusinessInterface. Essentially, the DAO pattern no longer exists, while its two functions are still put to work, separated by the advance of technology.

My proposed solution does not eliminate the layers, it merely lets the appropriate components take care of the previously combined functionality: JPA handles persistence (and only persistence) while the Service Facades handle business logic on a more pure level that old-school DAOs. The new solution still brings all of the benefits listed by Partington as part of the DAO pattern:
  • No direct dependency on the JPA api from client code.
  • Type-safety through the use of generics.
  • One logical place to group all entity-specific JPA code.
  • One location to add transaction markers, debugging, profiling. This feature is improved, due to the removal of the DAO layer.
  • One class to test when testing the database access code.
As well as the benefits listed under the Service Facade pattern:
  • Service Facades map DTOs to domain objects and back.
  • Service Facades function as the transaction boundary of your application.
  • The Service Facade pattern forces you to think about the interface of your application.

The DAO layer does not disappear, its functionality is divided between the Service Facade layer and the JPA provider.

Friday, July 17, 2009

Game Review: Crystal Clear for the iPhone/iPod Touch

I used to be a video games journalist for a short time. I brought a unique sense of laziness and ennui to a branch of journalism uniquely identified with the word "granola."

I now return, in force.

Crystal Clear is a game available through the App store to Apple whores customers. Billed as an "action game with light puzzle elements" it is very easy to pick up and start playing. Gameplay is very fresh, and quite unique. I really have no way of describing the gameplay in terms of a previous game, which is saying a lot. The best I can do is "Bejewelled family." Basically, you swipe or use multi touch to remove as many same colored "crystals" (not jewels!) from the board. The empty spaces are then filled from the top. You need to hit a certain score before time runs out, and then you progress to a higher level, which makes the same task more difficult. How does it make it more difficult? By reducing the time you have to reach the score and (presumably) increasing the score you need to advance.



That's all there is to it, but it leads into one of my only gripes with the game, which is there tend to be some hidden elements in the game, a big no-no for me. For example, I can see how much I've scored when I make a move. But the score bar that indicates my progress through a level has no numbers in it. Also, every time you make a move you get a "golden egg" in the upper right hand corner. These eggs actually multiply your score when you make the next move. The multiplier does go down with time, and there are also special jewels which will max out the multiplier at 5x and keep it there for a short duration. Problem is, its almost impossible to see which crystals are sparkly and which arent. Why? Well, your goddamned fat fingers are always in the way. The wonderful mechanics of the game are marred by the limitations of the platform. I obscure my own view of the playfield every time I make a move. WHY APPLE??? WHYyyyYYyyYYYYyy!?

The developer of the game cannot invent a better device, but he sure does push the technology in a direction I had not yet seen. Also, a futher note: the sound effects are excellent. I just regret that there is no music, but as most users will probably opt to play their own in the background anyway, its no big loss.

Graphics: 4/5
Sound: 5/5
Music: N/A
Gameplay: 4/5
Multiplayer: N/A
Overall: 4.3/5

Tuesday, July 14, 2009

Cyrstal Clear coming out soon!

This is a really neat game that will be available to iPhone and iPod touch users in about two weeks. Check out the video:

http://www.youtube.com/watch?v=YV31lxySnRo

Saturday, July 4, 2009

Singleton Pattern Icon

Singleton... or is it a coin? -_-


Command Pattern Icon

This one is definitely tougher than abstract factory, which has a real world representation. For you consideration, I present Command:



















The callback capability and main purpose of the Command pattern should be apparent. Missing are the undo queuing qualities. Oh well :)

Friday, July 3, 2009

Visualization of Design Patterns

Check this out:

So I have this idea. The idea is that some people learn visually, and if they could see design patterns, and associate the visual image with the concept, they would understand them easier.

To that end, I am going to try to iconify the GoF patterns. Here is the first one, Abstract Factory:

 
Clicky Web Analytics