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

  • Home
  • SEARCH
  • 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 153229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:45:51+00:00 2026-05-11T09:45:51+00:00

In my application, I’ve got several ViewModels that have a single service (repository, DAO,

  • 0

In my application, I’ve got several ViewModels that have a single service (repository, DAO, whatever), let’s call it the WidgetService, injected into them.

Let’s say that one of these ViewModels is a listing of all of a users widgets. Another could be the ViewModel for editing/creating a single one of these Widgets.

The user can view the list of widgets in the WidgetListView backed by a WidgetListViewModel and click a button to add a new widget. To create this new Widget, a CreateWidgetViewModel is new’d up and injected into the DataContext of some UserControl/Window thus, through the magic of DataTemplates displaying the CreateWidgetViewModel in a CreateWidgetView. Also, the newing up of the CreateWidgetViewModel does not necessarily happen within the scope of the WidgetListViewModel.

When the WidgetListViewModel it was injected with an instance of the WidgetService . The CreateWidgetViewModel was injected with this same WidgetService instance.

Now, when the user clicks save in the CreateWidgetView the Save method on the WidgetService will be invoked and the widget will be persisted. Now the WidgetListViewModel needs to be notified that there is a new Widget to be displayed!

The long buildup leads to this question: How do I let the WidgetListViewModel know that it needs to display the new Widget?

I’ve seen a Video in which a guy from Microsoft does this sort of thing using an event on the service that the ViewModel subscribes to. However, the downfall of this is that if the service outlives the viewmodel, then the viewmodel wont get GC’d until the service is GC’d. I could add IDisposable to the ViewModel. But then when/how to call Dispose when the ViewModel is only represented in the UI via DataTemplates?

Does anyone have any suggestions regarding this?

To clarify, I’d say my interpretation of MVVM most closely resembles Josh Smith’s. At least in as much as my MVVM architecture pretty closely matches that found in the Crack.Net source.

  • 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. 2026-05-11T09:45:52+00:00Added an answer on May 11, 2026 at 9:45 am

    Ok, given there’s been no answers to this yet I thought I’d give it a try, but I’m not an expert on MVVM.

    Events seem to be the way to go with this. However as you pointed out there can be a memory leak if the service outlives the ViewModel. The best way to deal with this is with a weak event listener.

    Weak Events allow you to hook up to an event with a weak reference so that if the source object is GC’d then the source is not kept alive by the event handler.

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

Sidebar

Related Questions

Application.Run(form); Actually I tried to call this from my project. I got this exception.
Application use NHibernate. I Have object A that contains set of objects B. I
Application db connection in web service. i haveto connect the db using that web
Application : HTA (therefore IE) This is an application that uses SendKeys to populate
An application I have recently started work on has to register two dll's, because
Application I work on generates several hundreds of files (csv) in a 15 minutes
Application-Stack: Rails3, CanCan, Devise, Shoulda I've got some nested Resources and want to test
The application I'm currently writing is using MVVM with the ViewModel-first pattern. I have
my application use 10 threads that to read a lot of html file.similar the
Application level events and processes in Excel are restricted to a single instance of

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.