Results 1 to 3 of 3

Thread: When is the best way to load initial data (MVVM, IoC, Unit testing)?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    When is the best way to load initial data (MVVM, IoC, Unit testing)?

    Hi!

    I am developing a WPF-application with the MVVM-pattern (no code behind). Yesterday I had a discussion with a colleague on where to load some initial data. Our viewmodel has a "PopulateList()"method that loads some data from the repository into an ObservableCollection property. We initialize every viewmodel and other dependencies in an IoC container (unity) But where to call PopulateList()? Basically, we have two choices:

    1) Call it in the viewmodel constructor
    2) Use a behaviour and hook up to the "onload" and bind the behaviour to a command, and execute the command (which points to the PopulateList Action)

    We have done it with a call in the construvot, but I see some problems with this

    1) Performance, if I have multiple viewmodels (say 20) and every one of them does some loading of stuff to their views, I want to use async/await. But that is not allowed in a constructor as far as I know for various reasons...

    2) Testability. I like to unit test my viewmodels, and having an external call in the constructor seems like a hazzle. I do want unit tests for "first load" scenarios, to make sure everything is hooked up properly (commands, properties etc), but I want to mock it. If I use behaviours, how can I then verify that the "repository.GetINitialData" was called or not, since the viewmodel command for this is executed only when the view is loaded? It is easier if I put it in the constructor... but somehow it doens't feel right to do async data loading in the constructor (async, ioc issues)

    Your thoughts?

    /H

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: When is the best way to load initial data (MVVM, IoC, Unit testing)?

    What is in control of constructing the VMs? If you have a controller to deliver them, then it makes sense to do it there. However, if you have one piece of generic code that loads any VM required that might not work.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: When is the best way to load initial data (MVVM, IoC, Unit testing)?

    The VM is constructed by Unity when the application starts.

    /H

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width