zombie
zombie

Reputation: 275

J2EE design pattern - How to make an application independent of backend service

I am trying to develop some RESTful web services which have to access some backend system for data. These backend sources can be any one of the folowing :

  1. Database through JDBC

  2. ERP, communicates through SOAP

  3. Another J2EE application, communicating through SOAP or maybe REST.

What best can I do at my level to have a loose coupling between my business layer and the DAO ? IS there some design pattern which may help me ?

Thanks a lot for the help !!

Upvotes: 0

Views: 1532

Answers (2)

jdearana
jdearana

Reputation: 1099

Hope this helps:

  • Layer your system: as you mention in your answer, always split bz logic from the data access logic. I would recommend to implement different layers in different binary modules, this will give you the flexibility to deploy different layers in different tiers (if you really need it).

  • Repository pattern: the business layer has abstract definitions of how to perform data access operations with certain entities in your bz logic. The methods in the repositories always use entities defined in your bz logic as parameters (the repositories always "talk" in the the bz language ). Once the repository interfaces are defined in the bz layer, you implement them in a separate layer: the data access layer.

  • Dependency injection: in the application layer (GUI, application server, ...), in the initialization logic (bootstrapping) you can use a dependency container to inject the repository implementations (the ones defined in the data access layer) in your bz logic. There a lot of open source frameworks with dependency injection containers.

With these 3 patterns, you can separate the DA logic from your bz logic.

Note that this is a very short answer, I would recomend you to take some time and read the following books:

Thanks,

Juanjo

Upvotes: 2

user7094
user7094

Reputation:

I hope I've understood your question correctly.

I'm working on an application at the moment which uses JAX-WS RESTful web services to access data. The data can come from a variety of different sources (two different databases and a Lucene index in our case).

I think, without knowing the specifics of your situation, I can only recommend the typical approach we use:

  • Code to interfaces, not to implementations. So create an interface for each DAO that you want, and have your business layer reference that interface rather than the implementation.
  • Use dependency injection (such as the Spring Framework) to configure and inject those DAOs, which can point to multiple data sources etc. That way the business layer has absolutely no knowledge of where the data comes from.

Although it's an old book, most of this info is in Expert One-on-One J2EE Design and Development. But I'd say these are standard Enterprise Java best practices.

Upvotes: 0

Related Questions