views:

56

answers:

1

I have a Java web application that has a 'disconnected' Java Swing desktop app. Using the desktop app users connect to the internet and download data they need from a server. They can disconnect and use the application offline. When they reconnect to the internet they can sync their data back to the server.

The server itself is a Java EE web application and the desktop app is a limited functionality version of the web app. Up until now all business logic and data access code was coded separately for each application. The data stores were different for each application (Web is SQL Server and Desktop is Serialized objects). Some new requirements involve a lot of development to both applications. Since the functionality is the same I want to layer the data access code and business logic so I can easily reuse it for both applications.

My plan is to do the following.

1) Create a DAO library (JPA)

Switch my DAOs (currently JDBC) to use Java Persistence API. This way I can start using a RDBMS on the desktop app like derby, sql express or something similar and reuse the entites and DAO code to do all of the db operations. I can bundle this into a class library and use the same library for the web and the desktop.

2) Create a library for business logic

The web app is written in struts 2. I will take all of the logic in my Action classes and but it in POJOs. Create a jar class library with the business logic. I can add the lib to my web and desktop project. Then I can call the POJOs from my struts actions and from my desktop application.

I assume that the JPA will take care of duplicating the data access code and putting the business logic (which for the most part are dao calls) in a separate library will take care of duplicating the business logic.

So my question is: What is a good strategy for seprating layers for an appication that can be used online and offline?

If you have any recommendations for achieving this differently, any warning for me as I begin this project or any frameworks that might help me please let me know.

Thank you.

+2  A: 

What is a good strategy for separating layers for an application that can be used online and offline?

Well, your plan looks decent from a high-level point of view and will definitely simplify the maintenance of your applications. I don't have much to add: create JPA entities, a service layer, and if you feel the need, a DAO layer1. Then use these parts in both of your applications, with different JPA settings.

Here are a few additional notes:

  • I would go for a full Java database on the desktop (derby), it will be easier to manage its lifecycle.
  • Spring would provide good overall support (for layering, for declarative transaction management).
  • JPA doesn't provide any facility for the merge or databases, you'll have to implement that yourself and handle painful things like conflicting changes, etc.

1 I would actually even consider skipping the DAOs and accessing JPA's EntityManager directly from the service layer. Some people might not agree with this but the fact is that the EntityManager already implements the Domain Store pattern which does pretty much what the DAO pattern does and and there is not much value at shielding it behind a DAO.

Pascal Thivent
Just to clarify when you say directly from the service layer you mean rather than calling from [Web -> Service -> Dao -> DB] I use [Web - Service -> DB]
@alzoid: I mean [Web -> Service -> EntityManager] rather than [Web -> Service -> DAO(EntityManager)]
Pascal Thivent