I am building an application in python with a python wrapper for WPF and with DAG support. I am currently at a point where I have to decide a consistent way of interacting between the data and the view.
As far as I see there are currently two obvious solutions.
First one is similar to how Android Applications are structured. You have a controller which sets/populates the view. So the controller owns the view and pushes only the primitive data that will be displayed. The view is just a dumb layer and has no idea of what's happening and where that data is from. And then if user interacts with the view, it will send callbacks to the controller (if it registered).
UserInfoController.py
userInfoView = UserInfoView()
userInfoView.onGenderChangedCallback = self.onGenderChangedCallback
userInfoView.setUserGenderValue(user.getGender())
UserInfoView.py
def setUserGenderValue(self, gender):
self.userGender = gender
def getView(self):
return ui.Label(self.userGender, onEditCallback=self.onGenderChangedCallback)
Second one is passing a (reference of) model to the view and letting the view to retrieve and update the data. The view now contains the model and therefore it can update it without any additional callbacks to the controller.
UserInfoViewModel.py
self.gender = 'Male'
UserInfoView.py
def getView(self):
return ui.Label(self.ViewModel().getGender(), onEdited=self.genderEdited)
def genderEdited(self, newValue):
self.ViewModel().setGender(newValue)
So I guess what I am asking is, should I pass the very primitive data and keep the view as generic as possible, then work with callbacks and do the business specifics in the controller.
Or should I pass the whole model to the view and let the view to update the model directly. This means that there will be less code to type.
PS. Do not judge the code - it's just for visualization.
EDIT:
Also to add - this application will be written in python which supports ducktyping. This means that with the second approach, the view is still reusable as long as the model meeting the required interface.