Let's say I have an application which eventually saves and retrieves it's data to/from DB. For the sake of explanation let's imagine the application deals with students.
I have a complete API that lets me work with business logic classes and seamlessly save/load them from/to the database. So I can instantiate a "Student", then call some Save(Student)
function, and it will be correctly saved into the DB (with all the connections to other entities correctly managed).
Now let's say I need to create a new window (view) in my GUI to implement some new business logic. My new view is going to be called from the main application window when the users presses some menu button.
I have created the view, the model (a class that performs the needed data manipulation and saves the results into the DB), and the controller (which acts as glue between the model and the view). It all works fine. Now my question is: when and where do I instantiate the model?
Currently I do it in the event handler of the main window, which opens my dialog, like this (pseudocode):
MainWindow::OnMyDialogRequested()
{
Model model;
Controller controller(model);
GUI gui(controller);
gui.exec();
}
The GUI is kind of stateless so it's ok if everything just dies when the user finishes working with it. When the GUI launches, it displays a splash screen, and (through the controller) asks for the model to prepare the data. When the GUI dies, it ask the model (through the controller too) to save the data back to the DB.
However, I'm concerned that instantiating the "model" and the controller this way might be a bad design decision. How do I do it properly?