I'm working on a rich client desktop application. I'm trying to use MVC to decouple the model, view and controller responsabilities. Problem is I can't seem to find a clean solution for handling communication between nested components.
Most of the answers I've read online were either confusing or were dealing with "web MVC" architecture rather than real MVC.
I do not mind ending up using another MVC flavor (MVP, MVVM etc...) if it helps me creating a clean a maintainable architecture.
Scenario
The codebase is small and contains two classes: MainView
, and MainController
. So far the view and controller responsabilities are decoupled and the code is clean.
At some point the codebase grows and I need a SecondaryView
with a button. When the button is clicked, the program needs to access the file system to perform operations.
Problem
How to handle the button click in a nested component (one level deep or more) without making a mess in my code?
What I tried so far
1 - One controller owning the nested view and a chain of observer/observables: unmaintainable code
MainController
has a reference to MainView
. MainView
has a reference to SecondaryView
.
When SecondaryViewButton
is clicked, SecondaryView
notifies its listener MainView
which notifies its listener MainController
which performs the file system operations.
This solution is even less maintainable when we nest components into components into components...
2 - Still one controller, but the main view now owns its nested views AND the controller
MainView
has a reference to MainController
and SecondaryView
.
When SecondaryViewButton
is clicked, SecondaryView
notifies its listener MainView
which directly calls performOperation()
method on MainController
.
This solution seems slightly better but it wouldn't work if the clicked button is nested several levels deep. Plus I suspect having a view reference BOTH controllers and other views is not a good idea?
3 - Multiple controllers, and each view has a reference to its controller
This solution seems better, but now all the controllers have to be able to access the file system (which is a common operation in this application) instead of a unique entry point when we just had MainController
.
I'm not sure whether this would be a good idea in the long run.
The real problem however is I don't have a clear understanding of how to nest components in a MVC architecture? Should I nest the views together and each view reference its own controller?
How would you solve the component nesting problem while maintaining a clean architecture?