Singleton vs Transient - What's the Difference in .NET Dependency Injection?

Поделиться
HTML-код
  • Опубликовано: 22 янв 2025

Комментарии • 53

  • @JamesMontemagno
    @JamesMontemagno  2 года назад +3

    Want to learn about Scoped dependencies? Checkout my followup! ruclips.net/video/HMN26wQ8J54/видео.html

  • @bkaankose
    @bkaankose 2 года назад +13

    Simple explanation James, thank you!
    One thing to note here is that (since it may not be clear with View-ViewModel example), avoid injecting transient into singletons. Because the object you define as 'short living or stateless' is now living in an object you define as 'global'. Your transient will live inside this singleton for the whole application lifetime and it may create thread safety issues.

  • @lindomagaye8729
    @lindomagaye8729 Год назад +1

    Thanks for such a great explanation, James. Since I started following you I'm learning a lot!

  • @jewersp
    @jewersp 2 года назад +2

    Great video. Nothing new to me, but it's great to actually see the differences in this way, it helps and confirms existing practices.

  • @faranahmadk7401
    @faranahmadk7401 Год назад +1

    Excellent. It cleared all the doubts. Thanks James.

  • @MotherOfTony
    @MotherOfTony 2 года назад +1

    very good explanation of dependency injection, thank you james!

  • @kopilkaiser8991
    @kopilkaiser8991 Год назад

    Shared, subscribed, and liked. You deserve it. All of it brother. Best wishes to you for your progression and may you excel to your very best.

  • @candidodmv
    @candidodmv 2 года назад +1

    Congrats James as always is comming awesome content!

  • @sam14032
    @sam14032 2 года назад +2

    Thanks ! Very easy to understand and super interesting.

  • @yeica
    @yeica 2 года назад +2

    Finally an example without GUIDs, thank you

  • @vickyfikriachri2311
    @vickyfikriachri2311 11 месяцев назад

    Thank for the video James. Super helpful.

  • @rikudouensof
    @rikudouensof 2 года назад

    Will add this in next weeked side project.

  • @MarcioViterbo
    @MarcioViterbo Год назад

    Awesome! In the last example where the page was defined as single and model as transient if cliqued on increase button certainly the number goes from 5 to 1. James should have done this.

  • @Namco78
    @Namco78 2 года назад +1

    Very good explanation, thank you for clarifying this.
    Great example, astonishing app 🙂

  • @christianzaragoza7575
    @christianzaragoza7575 2 года назад

    Que gran maestro James, te felicito por la excelencia de tus tutoriales

  • @hwod44
    @hwod44 2 года назад +1

    Great explanation. You could talk about STATIC variables inside Transient injections too. Thx James

  • @DavidDanosTech
    @DavidDanosTech Год назад +1

    Smooth. Nice!!!!

  • @4Asteria
    @4Asteria 2 года назад

    Very clear to the point.
    Thank you very much for your effort

  • @amnshumansunil3371
    @amnshumansunil3371 2 года назад

    Hi James can you please make a video on unit testing

  • @Ifænnn
    @Ifænnn Год назад

    how do I get back to a previous page but when a button is clicked?

  • @mattrobb3566
    @mattrobb3566 Год назад

    Thanks James. Was Using Maui for Windows App. Was using Transient for a Page which used Events (Events were enabled on appearing and disabled on disappearing). But noticed that the previously used transient Pages remained in memory and their event triggers triggered events in the New Page, so after leaving and returning to this page 3 or 4 times I was receiving 3 or 4 events when I should have received one!! Indicating the transient pages are remaining in memory! By switching the ViewModel and the the Page to Singletons this issue disappeared as would be expected. What I didn't understand is when transient pages are no longer being used why are they retained in memory?

  • @HackelSchorsch
    @HackelSchorsch 2 года назад +1

    Transient pages loaded from the shell do not behave transient. This is a bug which is already open for months.

  • @oktjona
    @oktjona Год назад

    can you do a video on how to connect netmaui app to sqlserver

  • @letsdoit7630
    @letsdoit7630 2 года назад +4

    Thanks for the explanation. I am used to c# windows application designs and familiar with binding the UI with a database like SQL or mySQL directly or via background code, but am struggling to understand the structure when it comes to MVVM and .net MAUI and linking to a hosted sql DB. I would appreciate if this is something you could give your opinion about. Thanks for the good content.

  • @PierreBritoM
    @PierreBritoM 2 года назад

    Amazing explanation!

  • @michael_azzer
    @michael_azzer 2 года назад

    Amazing explanation 👌

  • @michaelrogers6935
    @michaelrogers6935 2 года назад

    So when you register a class with a dependency injection service, and somewhere in the app an instance is being instantiated, does it automatically consult the service to see if there's an object? And does that apply to *any* object that's being created? And is the dependency injection service just storing the objects to be injected (the dependencies) or does it also store the objects that will be the recipients of the dependencies? So, if MainPage depends on a View Model, do you need to store both the Main Page and View Model, or just the View Model? [I feel like I am soooo close to understanding this 😀

  • @codingdave
    @codingdave 2 года назад +2

    Great video @james. When would you use scope in a WPF/MAUI application? Maybe when you open a file the application can work on?

    • @JamesMontemagno
      @JamesMontemagno  2 года назад +1

      Never, for web apps only docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection#scoped

  • @aslanamca8225
    @aslanamca8225 2 года назад

    Hello, I want to ask something I'm curious about.
    I create an instance of another class in a service registered as a singleton, with its new keyword.
    There will be only one instance of the singleton service within the application, but it will create another class inside it each time it is called.
    For example, if it is called 100 times, it will create 100 class instances. The last sample can be used but what about the previous 99 samples? Does the garbage collector detect and clean them or is this a memory leak issue?
    It's not the lifetime of the service I'm wondering about, it's what will be the class instances created in the singleton service.

  • @amrosalah01
    @amrosalah01 2 года назад

    does Xamarin packages still work with Maui ? things like Macha for background tasks for example

    • @JamesMontemagno
      @JamesMontemagno  2 года назад +1

      Some may be backwards compatible and some may to be updated, it depends on the package. On nuget you can see if it targets net6 then you know it is updated

  • @bernhardtarmann1254
    @bernhardtarmann1254 2 года назад +1

    Thanks James!
    I really understand Singeltons and Transients but I’ve got problems with the third type. What’s the secret of the Scoped instances?

    • @JamesMontemagno
      @JamesMontemagno  2 года назад +1

      Gotta wait until next week to find out tehehehe

  • @mojoojoseph8145
    @mojoojoseph8145 2 года назад

    Very concise.😁

  • @dfmf1982
    @dfmf1982 Год назад

    Hi James do you have a course in Udemy?

  • @jamesleonmiller4504
    @jamesleonmiller4504 2 года назад

    Thanks for explanation, James. Nice video.
    I am curios, what is practical difference between Get() and Resolve() when using introduced in Xamarin Forms, DependencyService?

  • @AdamsTaiwan
    @AdamsTaiwan 2 года назад

    Still to early in the day for me to get this.
    What I've been doing lately is creating ViewModels that function as services. I instance them using a script and keep the instance reference in a Dictionary. Every instance contains a GetView(string name) which can return any bound named view and a Process(string command) which allows me to script the object.

  • @FabioGomesCG
    @FabioGomesCG 2 года назад

    And AddScooped ?

    • @JamesMontemagno
      @JamesMontemagno  2 года назад

      For web projects only really docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection#scoped

  • @sheilainglin
    @sheilainglin 2 года назад

    AWESOME 👌 🆒️ 🙌 👍👍👍😁

  • @knockout4907
    @knockout4907 2 года назад

    Is Singleton per user or global for everyone?

    • @JamesMontemagno
      @JamesMontemagno  2 года назад +1

      Singleton will return the same instance when requested.

  • @BJBEAR989
    @BJBEAR989 2 года назад

    Great explanation of singleton and transient. What would you recommend in order to handle Android screen rotations in Maui? When using Xamarin a screen rotation caused the activity to be recreated which effectively destroyed the data in the view. I suppose my real question is does Maui preserve the contents of the view when the screen is rotated?

    • @JamesMontemagno
      @JamesMontemagno  2 года назад

      It preserves the screen state automatically. Android does by default now

  • @gwisekor
    @gwisekor 2 года назад

    6:40 어렵지만 중요한 내용. 난 그냥 new를 쓰고 싶다. ㅎㅎ

  • @kishant4565
    @kishant4565 2 года назад

    Need to create a pdf / printable page like receipt printing /barcode stickers printing using .Net Maui, please help.

    • @JasonPurkiss
      @JasonPurkiss 2 года назад

      just fyi when I want to create pdf's in Xamarin I open a web page from the app and send the app data to the web page and then save as pdf, a lot simpler, let me know if you find a better and cheaper way :) as not the fastest and easiest solution :( I tried all the other solution's on the net but couldn't get on with any of them

  • @sakabas28
    @sakabas28 10 месяцев назад

    JamesMontemagno => GEMSMontemagno ♥

  • @NK-xw4uu
    @NK-xw4uu 2 года назад

    By "request" do you mean a http request or do you mean request for an object?