dsuryd / dotNetify

Simple, lightweight, yet powerful way to build real-time web apps.
https://dotnetify.net
Other
1.17k stars 164 forks source link

Dispose for Transient instances #296

Closed Paulskit closed 3 years ago

Paulskit commented 3 years ago

Problem: Transient instances don't get disposed on VM dispose

Steps to Reproduce: 1) Register service as Transient services.AddTransient<IServiceA, ServiceA>();

2) Inject in ViewModel

public class MyViewModel : BaseVM
{
   private readonly IServiceA _serviceA;

   public MyViewModel(IServiceA serviceA)
   {
       _serviceA = serviceA;
   }
}

Since it's transient service, it gets created on every instance of MyViewModel. However, it doesn't get disposed when model disposed

dsuryd commented 3 years ago

Transient and scoped objects have the same lifetime as their connections and not view models, so they will only get auto-disposed when the connection terminates. If you need it disposed along with the view model instance, explicitly dispose them in the view model's Dispose method override.