Comments (6)
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 new request/refresh of the page but why would one refresh the page to call Dispose?
The disposal of a resource is determined by the lifetime of a resource. Usually you would use OwningComponentBase to circumvent this issue. However, having this as a base class has some implications that are not feasible when trying to use this library. This is indeed an issue but this cannot be resolved by just implementing IDisposable
. The DI needs to have control over this in order to keep track of the instances. Manually implementing the dispose pattern could result in disposing a singleton which leads to unpredictable behavior.
If you have a good solution for this problem, I'll gladly take it.
from mvvmblazor.
Well Implementing IDisposable in viewmodel(Service) with transient dependency would further cause memory leak as GC stores the reference for those objects with IDisposable type.
thus I think the possible solution could be
- Have a PageClosed event in ViewModelBase
- Call the PageClosed event when the BIndings Are Disposed.
Thus in this way one can do the clean up activity e.g Deregister events etc at the time of page close.
If you think this could solve the problem, I would happy to raise PR
from mvvmblazor.
That is the reason why OwningComponentBase
would be the right choice. Implementing this behavior on top of the Blazor mechanics is an anti-pattern IMHO. Resolving the memory leak for transient services is out of scope of this library. The issue of transient services implementing IDisposable
should be handled by some analyzer provided by Microsoft. I would rather provide an extension method that registers the view model in a proper way (meaning always scoped) to prevent this problem from happening.
from mvvmblazor.
How about having a Page Closed event in MvvmComponentBase which can be invoked when the page is Disposed thus one can do any cleanup activity on page close?
In this way we could avoid IDisposable in ViewModel
from mvvmblazor.
@klemmchr like you suggested we could have one more class "MvvmOwningComponentBase" which inherit from OwningComponentBase just like the way you had created MvvmComponentBase for blazor framework's ComponentBase, we could have this new class as well.
from mvvmblazor.
Yes, this is the only proper solution to this problem. I will address this together with #52 where the ground work for this is already set. This will also come with a breaking change so I will take this opportunity to upgrade it to .NET 6 while at it.
from mvvmblazor.
Related Issues (20)
- Guidance on Two-Way Binding on a Model property HOT 6
- Cannot use Blazored ILocalStorageService inside ViewModel HOT 2
- Blazor Server - No Startup HOT 5
- MvvmDialogs HOT 1
- Scope management enhancement HOT 7
- MvvmComponentBase does not call AsyncServiceScope.DisposeAsync() HOT 1
- Allow custom EqualityComparer on ViewModelBase.Set<T>() function HOT 1
- Parameters with non-public accessors are not set HOT 9
- Switch to CommunityToolkit.Mvvm HOT 2
- Layouts? HOT 11
- Remove constructor for generated components HOT 2
- Razor page dispose not firing when inheriting from view model HOT 4
- Why is the lifetime of scoped view models tied to the component lifetime instead of the request lifetime? HOT 2
- How can an MvvmComponent implement IAsyncDisposable? HOT 4
- Index out of bound exception
- Feature request: "MvvmOwningComponentBase" which inherit from OwningComponentBase HOT 1
- Update Service Locator Pattern in MVVMComponentBase to Constructor Injection HOT 1
- Can the CodeGenerators be bundled with the nuget package? HOT 5
- Is there a way to InvokeAsync within the view model? HOT 9
- After upgrade to version 6.0.3 scoped dependency injection is not working properly HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mvvmblazor.