In [step3] we have developed a Simple Client bundle which consumes the UserService from the OSGi services registry to display a list of User in the console. So we have seen how to:
- consume service with Spring DM <osgi:reference.
- publish service with Spring DM <osgi:service.
At this step we have a service layer. Now we can add Dao layer (Some people prefer merge services/dao layer, but in our case I prefer do that to have a facade and it will help us when remoting with REST will be done) to retrieve User data from Database. To do that we will use :
Before doing that, I would like show you an idea that we have done in our Eclipse RCP/RAP XDocReport application. We are 2 developers and when we wanted to integrate JPA as Dao implementation we would like continue to work together on 2 tasks :
- one developer integrates Dao JPA implementation with EclipseLink and Spring Data JPA (an hard task when you don’t know very well those technologies).
- one developer continues to develop the application: business services, UI components, etc…
The idea was been to integrate DAO API (very easy task) in our services layer and uses Mock Dao. So we have :
- a Mock Dao implementation layer: which works with Java Map.
- a JPA Dao implementation layer: which works with EclipseLink and Spring Data JPA.
Mock Dao was easy to develop (you will see that in this article) and once time UserDao API was finished, we can work on our own task (one task for JPA and one task for continue the development of the application) without disturb. You can tell me, yes it’s a classic mean to use Mock object.
But the second problem was Data. To inject data (ex: in our case User data) :
- for Mock Dao, Data is managed with Java.
- for JPA Dao, Data is managed with SQL scripts…
However JPA gives you the capability to generate Database by using the JPA annotation of the Domain classes (without SQL scripts). What is about data? So we tell us, why we cannot use ours services save method to inject data? For instance in our case we could call UserService#saveUser(User user) which uses Dao (Mock or JPA) somewhere to inject our users :
- it will work for Mock Dao.
- it will work for JPA Dao.
But where can we do that? The idea is very simple. We have created a new bundle “datainjector” with a DataInjector class which consumes service to inject Data. For instance in our case we could have that :
public class DataInjector {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public void inject() {
userService.saveUser(new User("Angelo", "Zerr"));
...
}
}
In this article we will do that :
- add API Dao layer used in the Services Implementation.
- implement DAo with Mock (Java Map).
- inject User data in a “datainjector” bundle which will use the UserService.
Lire la suite…