How should I pass a reference of an object to a controller in MVC?
-
10-07-2019 - |
Question
I'm trying to get better at using MVC/MVP style patterns with my WinForm apps and I'm struggling with something that maybe someone here with more experience can help me with.
Below is my basic project layout:
The class G2.cs
handles the running of various threads and includes a Start/Stop and other various methods involved with those threads. It is my "main" class I suppose. It contains the main loop for my application as well.
My GUI is composed of 3 forms so far and an associated controller for each. The MainForm
has Start/Stop buttons that need to call methods on my G2
class as well as possible future forms.
What would be the correct way to pass a reference of my G2 class to all the view's controllers who need it? Do I need to pass the G2
reference to the Form when I create it and the form in-turn passes it to my Controllers or... is that not a good way to handle things?
Also, am I correct in that it is the Views responsibility to create an instance of it's controller and it "owns" the controller?
Solution
If G2 is main program class, and exists only in one instance, I would use singleton pattern on this class - no need to passing references.