Giter Site home page Giter Site logo

Comments (6)

klemmchr avatar klemmchr commented on May 22, 2024

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.

deveshbahuguna avatar deveshbahuguna commented on May 22, 2024

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

  1. Have a PageClosed event in ViewModelBase
  2. 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.

klemmchr avatar klemmchr commented on May 22, 2024

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.

deveshbahuguna avatar deveshbahuguna commented on May 22, 2024

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.

deveshbahuguna avatar deveshbahuguna commented on May 22, 2024

@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.

klemmchr avatar klemmchr commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.