I have a Java Spring-boot application which has layers(packages):
- Controller(controller class)
- Service (business logic)
- Domain (database layer)
Inside My Domain
I have two database classes
- MysqlDBManager
- MongoDBManager
I have many classes in my service package
which uses methods of both MysqlDBManager
as well as MongoDBManager
.
If I use instances of MysqlDBManager' and
MongoDBManager` on my service class directly then service layer will get tightly coupled with domain layer.
To solve this problem I have created a class named **DBManager**
in my domain package which behaves as a proxy class which calls MysqlDBManager' and
MongoDBManager` based on the use case internally.
Now service layer, instead of calling MysqlDBManager' and
MongoDBManagerdirectly they call
DBManager, which internally calls
MysqlDBManager' and MongoDBManager
.
Hence now my service class is not tightly coupled with domain layer.
Below are my classes which resembles my use-case.
MongoDBManager class:
Class MongoDBManager {
public saveUserDocs(String docs) {
//.....
}
}
MysqlDBManager class:
Class MysqlDBManager {
public saveUserData(List<String> rows) {
//.....
}
}
DBManager class:
Class DBManager {
@Autowired
private MysqlDBManager mysqlDBManager;
@Autowired
private saveUserDocs mongoDBManager;
public saveUserData(List<String> rows) {
mysqlDBManager.saveUserData(rows);
}
public saveUserDocs(String docs) {
mongoDBManager.saveUserDocs(docs);
}
}
Service class:
class Service {
@Autowired
private DBManager dBManager;
//....... code here
dBManager.saveUserData(rows);
dBManager.saveUserDocs(docs);
}
My Question is:
Is this (as explained above) a good way to make my application layers loosely coupled or their exist better ways to do this?
Aucun commentaire:
Enregistrer un commentaire