1

I work on a quite big software, with litterly over 100 dialogs. We are considering MVVM light, but I just read an old artikle which states that "All ViewModels are kept im memory till the cleaup is called". So my questions:

  • Does that mean that on load it will load all ViewModels in the program?
  • If it is the default to load all, can I change it to load/instantiate the ViewModels only when the dialog is opened?

The article is Abkürzung zum ViewModel. Sorry but it is from 2008, in German and you need to register to read it. But my question is only about that statement, so you may ignore it.

I have read this question, it was very informative but I didn't really find an answer to my question.

Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69

1 Answers1

1

This is the default ViewModelLocator class created by nugetting the mvvmlight package.

public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        ////if (ViewModelBase.IsInDesignModeStatic)
        ////{
        ////    // Create design time view services and models
        ////    SimpleIoc.Default.Register<IDataService, DesignDataService>();
        ////}
        ////else
        ////{
        ////    // Create run time view services and models
        ////    SimpleIoc.Default.Register<IDataService, DataService>();
        ////}

        SimpleIoc.Default.Register<MainViewModel>();
    }

    public MainViewModel Main
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

    public static void Cleanup()
    {
        // TODO Clear the ViewModels
    }
}

There is no magic creating any viewmodel inside that class unless you put it in there. By default there is a ServiceLocator involved, which you can remove and replace by any DI container of your choice or do the instantiation of the ViewModels handled by this ViewModelLocator manually.

The static Cleanup() is not called by default and you have to place the call whereever you like to get it called whenever you like. That is all up to you if that is needed at all.

Conclusion: It is all up to you how the ViewModelLocator will operate and behave

To answer your questions:

  • Does that mean that on load it will load all ViewModels in the program?

Nope, unless you write code that will do so

  • If it is the default to load all, can I change it to load/instantiate the ViewModels only when the dialog is opened?

(see above)

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
  • Hm, but using SimpleIoc Containers in there, Viewmodels will be created lazy, right? At least I thought so. – Fildor Aug 25 '17 at 13:52
  • Did a quick research myself: There is [void Register( bool createInstanceImmediately )](http://www.mvvmlight.net/help/WP8/html/a1995dc0-d1ba-4b80-553d-a3401bbc1f65.htm) with which you can force immediate creation. So I guess the default is lazy (the docs don't explicitly state that, as far as I read). – Fildor Aug 25 '17 at 13:56
  • 1
    As I said: You can create the instances manually or delegated to any DI container of your choice => everything is under your control – Sir Rufo Aug 25 '17 at 14:37
  • There is no rule when to call that method. I guess in most cases it will never be called. So never mind about it, unless you face a situation where such method would be handy. – Sir Rufo Sep 16 '17 at 07:14