|
Integrating Flex with the Persistence Layer
In applications that use a well defined decoupled architecture over the Web you do not communicate with your persistence layer directly. Using Flex should not change this architecture. The integration layer typically will communicate to your persistence layer in most cases. This is usually done using a Data Access Object (DAO) that is responsible for accessing data in a persistent store such as a database. The Flex client should not communicate directly to the persistence layer or even know about this layer because it forms a tight coupling. Let's use Hibernate as an example of our persistence layer.
There are a couple of pitfalls when using Hibernate and remote objects with Macromedia's AMF gateway. Hibernate users know you cannot access a lazy loaded collection that has not been initialized with a Hibernate Session object. Accessing a collection of dynamic proxy objects that has not been initialized will result in a runtime exception. The AMF gateway does not know how to specifically look for Hibernate dynamic proxy objects. A potential solution is an aspect-oriented programming (AOP) interceptor that can take the object that is about to be sent over the AMF gateway in the delegate object and remove the dynamic proxies. This process involves sending the resultant object through the interceptor class, which recursively looks for proxy objects that use reflection and that are not initialized. If any lazy proxy objects or collections are found then they are set to null. This is a cross-cutting concern that can be applied as an aspect using an AOP language such as JBoss AOP, AspectJ, Spring AOP, and so on. The AOP interceptor should be applied to objects in the business delegate layer. Figure 4 shows what this looks like in the application architecture:
Introducing AOP interceptors/advice to delegate objects before they pass through the AMF gateway
Figure 4. Introducing AOP interceptors/advice to delegate objects before they pass through the AMF gateway. This further reduces coupling from components in higher layers such as the integration layer, persistence layer, and so on.
The good news is the AMF gateway does know how to cache bidirectional objects so infinite recursion does not occur when transforming the objects. Therefore, you can keep these relationships intact when transmitting them back and forth over the AMF gateway. Also, because the objects are disconnected and made into copies, you will need to use the Session.saveOrUpdateCopy(Object object) method to persist results into the database. This method has to be used because the object that will be sent back through the AMF gateway will not have any enhanced bytecode information that Hibernate can take advantage of. |
|