I am trying to implement architecture with multiple MVVM components. In every component I need to use MVVM pattern because it best suits me. Every component must have abilities to use another component. Component contains only View and ViewModel, because models are added to component when it is necessary component to work with certain Model.
For example we have the following screen
--------------------------------------
| Component A |
| |
| |--------------| |------------| |
| | Component B | |Component C | |
| | | | | |
| |--------------| |------------| |
|------------------------------------|
Screen contains three components. For example Component B is login form, so, I would have something like
public class ComponenentB {
private ViewB view;
private ViewModelB viewModel;
private Component parent;
private List<Component> children;
public void addListener(...) {..}
public void removeListener(...) {..}
public void fireEvent(...) {..}
public void setCredentials(UserCredentials creadentials) {
model.setCredentials(creadentials);
}
public UserCredentials getUser() {
return model.getCredentials();
}
//+ setters and getters for parent and children.
}
public class ViewB {
private ViewModelB viewModel;
private TextField username;
private TextField password;
}
public class ViewModelB {
private ComponentB component;
private BindingProperty username;
private BindingProperty password;
private Command login = () -> {
login();
component.getParent().doSomeTask();
}
public void setCredentials(UserCredentials creadentials) {..}
public UserCredentials getUser() {...}
private void login() {...}
}
So we see, that View and ViewModel are hidden. Component modifies them via its methods - ComponentB.setCredentials(creadentials), ComponentB.getCredentials(creadentials).
This is in theory. However, I have feeling that something is wrong here. The reasons are:
- I don't understand where to place
private Component parent; private List<Component> childrent;
and their getters/setters to Component or to ViewModel. - It is seems strange to me that in ViewModel via component reference I get access to other components.
So, the main problem in my solution is that roles of Component and ViewModel are not clear defined.
My question - where are my mistakes and if I do everything wrong what solutions/patterns are used in such cases with MVVM?
Aucun commentaire:
Enregistrer un commentaire