Hello,
I’m trying to organize my application in a 3 layer achitecture, as suggested in the book of Vaadin. What I’m doing is:
- First layer for UI components
- Second layer for beans
- Third layer for data management (communication with the database)
I am creating a specific package for each of these layers.
The principle described in the book is that each layer can be dependent on the next one, but not the other way aoound. So layer 1 can depend on layer 2, but layer 2 should not depend on layer 1.
Now, I have a DBManager class which takes care of all DB queries, and holds the connection to the DB. This class is included in the third layer, as it is database related.
And taking the user management in my application as an example, I have created a User bean in the second layer, and a userData class in the third layer. The userData class handles all DB communications for user related stuff (insert a new user, retrieve user data, etc…). So, the idea is for the User bean to have a userData object as a variable. And the userData, in turn, makes use of the DBManager class described above, as it is the one holding the connection to the DB.
My problem is that I am not able to achieve this architecture while maintaining the layer principle above. My DBManager object must be common to all, because it holds the DB connection, So I am creating a specific instance in the main UI, which will be shared across the application
//FIRST layer
public class MainUI extends UI {
DBManager db = new DBManager();
UserBean user = new UserBean();
}
//SECOND layer
public class UserBean{
UserData userData;
public UserBean(){
userData = new userData();
}
}
Now, in the userData clas of the third layer, I would need to use the particular db variable of the particular UI instance, like:
[code]
//THIRD layer
public class userData{
DBManager db;
public userData(){
db = ((MainUI)getUI()).db
}
}
[/code]But since the userData class is not a component (nor is the UserBean), I actually do not have the getUI() method, so I need to pass the UI as an argument from the first layer. It would have to be something like this:
[code]
//FIRST layer
public class MainUI extends UI {
DBManager db = new DBManager();
UserBean user = new UserBean(getUI());
}
//SECOND layer
public class UserBean{
UserData userData;
public UserBean(UI ui){
userData = new userData(ui.db);
}
}
//THIRD layer
public class userData{
DBManager db;
public userData(db){
this.db = db;
}
}
[/code]The problem with this is that I am braking the layer principle, because the second layer is actually dependent on the first layer, and the third one depends on the second one. And this would happen with every bean I create with its correspondent data class (for example, carBean and its carData, etc.). So this leads me to think that perhaps this is not the best approach. Is there a prferred way to organize this?
Thank you very much.