Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7505381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:53:33+00:00 2026-05-29T21:53:33+00:00

In my WPF application I use the MVVM pattern together with dependency injection. The

  • 0

In my WPF application I use the MVVM pattern together with dependency injection.

The ViewModels that prepare data from the database get the repository injected into the constructor. They also populate the properties with the data from the repository in the constructor.

The ViewModels are all created in the static constructor of the ViewModelLocator class that all Views use to bind to their ViewModel.

This has the following disadvantages:

  1. The data in the Views is never updated, not even when closing and re-opening them, because the ViewModel instance is always the same.
  2. Upon opening the first view, all ViewModels are instantiated and the data they require loaded from the database.

I can think of two ways to solve these problems:

  1. Make every ViewModel implement a method that reads the data from the database and initializes the properties – instead of doing so in the constructor. This would require calling that method every time a view is opened. This introduces temporal coupling which I don’t like.
  2. Implement the ViewModelLocator in such a way that it creates the requested ViewModel each time the corresponding property on the ViewModelLocator is called. I don’t like this method, because my composition root wouldn’t be executed at the start up of the program but would be spread throughout the life time of the program instance.

Is there another way to solve this problem? How are others solving this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:53:34+00:00Added an answer on May 29, 2026 at 9:53 pm

    Implement the ViewModelLocator in such a way that it creates the requested ViewModel each time the corresponding property on the ViewModelLocator is called.

    This is more of the approach that I typically take in situations like this. However, instead of having the ViewModelLocator composed via DI of ViewModels, I compose factories that create the ViewModel.

    I don’t like this method, because my composition root wouldn’t be executed at the start up of the program but would be spread throughout the life time of the program instance.

    This gets “solved”, at least partially, by having the composition compose factories instead of the types themselves. The composition happens once at startup, but creation can occur at any time of the ViewModel in question.

    For example, using MEF, you can switch your imports around to use ExportFactory<T> instead of their type directly. Along with a NonShared Creation Policy, you can construct ViewModels, as needed, and always work with fresh data, without the temporal coupling problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use MVVM in my WPF application which displays data from
I want use MVVM design pattern in WPF and Silverlight Application. Where can i
I'm writing an application in WPF using the MVVM-pattern and will really often use
I'm creating a WPF application using the MVVM design pattern that consists of a
I use the MVVM-pattern in my WPF-application. Now I show a dialog with my
I designed a WPF application based on MVVM pattern and I need to get
I'm taking an vanilla WPF application and converting it to use the MVVM pattern.
I am trying to adapt a simple WPF application to use the Model-View-ViewModel pattern.
I've been trying to use the WPF toolkit in a WPF application that I'm
I'm trying to use the FolderBrowserDialog from my WPF application - nothing fancy. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.