Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Disposing ViewModel automatically in MVVMComponentBase #55

Closed
deveshbahuguna opened this issue Nov 19, 2021 · 2 comments
Closed

Disposing ViewModel automatically in MVVMComponentBase #55

deveshbahuguna opened this issue Nov 19, 2021 · 2 comments

Comments

@deveshbahuguna
Copy link

deveshbahuguna commented Nov 19, 2021

Hi Folks,
I was using this library and wanted to unregister some events inside Dispose method of VIewModel but could not find a best way to call it.
So I did some changes inside MVVMComponentBase to call the Dispose method of the Binding Context if it is disposable.

Should I raise a PR as this might be useful for others as well or do you have any better suggestion?

Below is the PR: #56

@klemmchr
Copy link
Owner

The view model gets injected into the component via DI. Injected services should never be disposed as docs say:

The container calls Dispose for the IDisposable types it creates. Services resolved from the container should never be disposed by the developer. If a type or factory is registered as a singleton, the container disposes the singleton automatically.

This means that the component should never dispose the view model. Just implementing IDisposable in your view model is enough to make this work.

@deveshbahuguna
Copy link
Author

deveshbahuguna commented Nov 21, 2021

@klemmchr You are right as per the documentation one should not call Dispose but if you look at the example provided by microsoft, the dispose method is called on each refresh of the page but why would one refresh the page to call Dispose? and during my testing as well the Dispose method is called on Page Refresh.
"The debug console shows the following output after each refresh of the Index page:" ~Microsoft

In my View there could be two way to solve this problem

  1. via Dispose
  2. Invoking an event say page closed in view model on dispose.

What do you think?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants