0

How should I structure a start up class that runs at the start of the application and loads the data in?

What is the most recommend to do in order to fill in the data into the collections of the viewmodel when you start up the program? What I want to do is to make the data load in the background, while the spinning bar of the program is running.

Should I create a start up class and call on this in the app.xaml.cs file?

  • I think you should search about how to implement singleton class it will help you with this – Shehab Jul 22 '19 at 08:57
  • I'm using dependency injection if it would help? :) –  Jul 22 '19 at 09:11
  • You can write [Main() method manually](https://stackoverflow.com/q/2694680/1997232), display some splash window, load data, close splash and continue with main window. – Sinatr Jul 22 '19 at 09:22
  • I have created a Startup.cs class, that call on LoadData function to the different ViewModels. Is this an okay approach? I'm also wondering where should the LoadData logic be placed? The LoadData logic just call on methods from the DataAccessLayer and then assign it to the viewmodel collection. I'm calling on the Startup.LoadData(); in App.xaml.cs –  Jul 22 '19 at 10:38
  • You would typically inject the model into the startup view model using construtor injection. The bootstrapper (or startup class) will: 1) start the splash screen 2) initialize the DI container 3) the view model constructor is invoked and does the required initialization using the passed in model argument 4) After application construction the DI container is finished 5) close the splash screen 6) get the main view instance with the injected view model and show it. – BionicCode Jul 22 '19 at 11:18
  • Right now I'm doing all this in App.xaml.cs. I'm calling for the MainViewModel, then showing the mainwindow, after that I start the startup.Run(); task. Would this be fine? –  Jul 22 '19 at 12:28
  • @BionicCode Right now I'm doing all this in App.xaml.cs. I'm calling for the MainViewModel, then showing the mainwindow, after that I start the startup.Run(); task. Would this be fine? –  Jul 22 '19 at 15:00
  • I don't know enough details. The view model should initialize itself on construction. The constructor must take an interface to the model's `DataAccessLayer` for interaction. So `LoadData` should be called from the constructor of the view model. Finish construction of the application's dependency graph before you show the main window to prevent the user from interacting with the application before it is ready. – BionicCode Jul 22 '19 at 15:30

0 Answers0