Dependency Injection in .NET Core (.NET 6)

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

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

  • @themateoo
    @themateoo Год назад +27

    You are the best instructor on youtube. You just understand what have to be told to make people to understand the topic. It's a gift that few people have.

  • @SergeiFedorov77
    @SergeiFedorov77 2 года назад +30

    Tim, you're amazing at explaining all these code related functionality. Thanks to your pedagogic skills I've actually understood (I think) things I almost given up on understanding.

  • @BrunoTorres05
    @BrunoTorres05 2 года назад +36

    what a timing! that's exactly what I was looking for, thank you so much.

  • @BonnieMan42
    @BonnieMan42 2 года назад +6

    Came here to consolidate my understanding of .NET DI, left here also learning some great info about logging including your string interpolation tip. Thanks Tim 😊👍

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

      I am glad it was so helpful.

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

    So called complex topic explained in a super simple manner. You're awesome

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

    I tried to understand DI for so long, finally got it thanks to you

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

      I am glad it was so helpful.

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

    I vote for video to demostrate how to setup dependency injection for console app. It would round-out this discussion. Thanks!

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

    Best video on DI in .Net 6.0. It helped me to understand DI in better way.

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

      I am glad it was so helpful.

  • @sethamballa9428
    @sethamballa9428 Год назад +11

    Great video! A suggestion would be to use concrete, practical names for folders and classes, rather than just "Logic" and "DemoLogic". Will help the viewer understand a lot better IMO

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

    bestest free course I am ever watching. Thank you very much

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

    51:00 Great video! I wanted to give another option for injection of different implementations of an interface and that is to use a factory pattern. So you would inject a factory class that can produce those types of interfaces. That is how I have done it. My specific implementation was that I needed to validate data that has different structures but one common field that defines the pattern used so I use that as a parameter to the factory method. Each of the validations are available in the container and the factory uses the ServiceProvider to extract the interface.

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

      My thought also.

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

      That can be a good solution when needed, but I would encourage you to avoid it as much as possible. Once you start down that road, it can be easy to justify doing this for everything. Next thing you know, you have a complex DI/factory system that is a nightmare to maintain. It is an added complexity that you don't often need.

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

      @@IAmTimCorey What would you guys recommend for reducing setup complexity? Specifically, I'd like to abstract data access, because I want to keep my options open as to what kind of data storage is going to be used. Let's say it's either MSSql or SQLite. So there would be an IUserData interface for user data CRUD and concrete UserDataMSSql and UserDataSQLite implementations. So far so good, but this is going to grow quite a bit as I'm adding more data models. Would you just write a big if-statement handling the DI setup, depending on storage type? Use a factory? Or maybe put this in the app settings somehow? Thanks! --- EDIT: Looks like this kind of setup can be simplified with generics:
      services.AddSingleton(typeof(IDataStorage), typeof(SQLiteService));
      then requesting a concrete service from DI with
      IDataStorage service = App.Current.Services.GetService();

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

    Tank you Tim it was the best Dependency injection course i've ever seen.

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

    Hey Tim. Another great presentation. I learn something new every time I watch your videos. Thank you for the service you provide the community

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

      You are welcome.

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

      It's sometimes little side effects, like writing ctor to create a constructor, or a semicolon after the namespace, something not really the intended message of the video, but still useful. Everybody can discover something valuable. Even if you think you know it all.

  • @AdamThomson
    @AdamThomson 2 года назад +5

    Incredibly useful as always. This answered a whole bunch of questions I had and I loved the discussion about the pros and cons of various approaches.

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

    My main complaint is that I never found your videos before! Succinct, well thought out and a great, clear voice. What more could one ask for?

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

      I’m glad you enjoy them.

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

    I recently ran into the Singleton vs. Transient use for a project I started with Tim's recent Open API video where Singletons are used for the database access. But the tutorial I followed about Bearer Tokens and Refresh Tokens used Transients, and I had to look up the difference. Great video showing the difference and more info on DI.

  • @swedishprogrammer
    @swedishprogrammer 2 года назад +18

    Dependency Injection is amazing, but have problems with unit testing it with mocked objects. It would be amazing if you make a video about dependency injection's increased testability. Thanks for the C# content, like always!

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +9

      Do you mean beyond what I covered here? ruclips.net/video/DwbYxP-etMY/видео.html
      If so, please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

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

      .

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

    No words are enough to say how great info this video is. Just gratitude 💯💯💯💯

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

    Thank you Tim.
    Your videos are very useful. I really appreciate your efforts in helping us.

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

      You are welcome.

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

      @@IAmTimCorey Hi Tim, I am a big fan of your video and would like to ask to see if you have any videos/materials talking about using C# and .NET to build standalone desktop application? In particular, I am very interested to use C# to do some data processing and chart plotting and making it a standalone tools for the engineers. Thank you very much!!! We can talk offline as well

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

    That's what am looking for loooong time . special thanx for you Tim Corey

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

    Very Clear Description for DI Thanks TIM

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

    That's really cool! Finally I've understood it ! Using Random values for explanation is really great, clearly I've got the picture of DI ! ( I should have watched your video before!) Thanks a lot!

    • @IAmTimCorey
      @IAmTimCorey  Год назад +2

      Glad it helped!

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

      @@IAmTimCorey absolutely helped! Thanks! Going to check your courses!

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

    Thanks for a great videos. This is really filled a lot of topics I missed on Dependency injection.

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

    Mind-blowing explanation, every single second of this video is information information and information ⭐⭐⭐⭐⭐

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

      Glad you liked it!

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

      @@IAmTimCorey not only that, watching many of your videos related to C#. 👑One of a kind👑

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

    Thank you sir, you are my guy for C#! Thanks for sharing the knowledge!

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

    Thanks a lot for this very good presentation Tim. We need to stay focus regarding the number of interesting point provided ;)!

  • @navid-wu6bx
    @navid-wu6bx Год назад

    Dear Tim, your video helps me a lot, you'r incredible!!

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

    very well info on Scoped, that help me understand it better! Thank You!

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

    Outstanding explanations! As usual.

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

    Best explanation and I just started to looking for DI, so I'm really happy that i run into the best one at first, it's not something happening all the time when you are Junior 😂. Simply the best, Tim! Thanks man!

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

    You just saved my mind from a lot of frustration, thank you.

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

    Excelent content about DI in net core, Thanks Tim for sharing this information.

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

    I am looking for dependency Injection in WPF that's ll be great
    Thanks for the videos by the way. SO much to learn from

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

      Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

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

    Best explanation on web!!!

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

    Great video, looking forward for more .NET > 6 !

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

    How would you go about creating a multi-tenant .Net 6 MVC website with single code base/separate database per tenant, where most users will have access to a single tenant but some users will have access to multiple tenants? I want to use the same host name for all tenants in the event I need to use 3rd party controls that cost extra for additional hosts.

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

    Thanks Tim for the wonderful content, easy to understand

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

    Really Excellent tutorial .. thanks for your effort

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

    Tim - would be great to see an updated video on how to set up DI for typical console apps in the .NET Core model (versus the old 2020 version).
    I have web apps that use the same master data over a session. I've been using ProtectedBrowserSession for this, but would it be better to use Scoped DI for this data? What are the best ways to make the call as to approach on this?

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

      I have the same question.

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

      I'm not sure if I understand. You are asking about a console app but your example is a web app. I know that web apps are just console apps that have additional things on top, but that's a key distinction. I have a video here on RUclips that shows you how to add dependency injection in a console application. I also have videos showing you how to use dependency injection in a web app (this video included). As for saving your data over a session, I can't really give you a specific answer since there is a lot more questions to ask. However, in general you should use the web project type that best fits your needs. For example, Blazor Server is very good at having data over multiple calls (maintaining state). So is Blazor WebAssembly. MVC, Razor Pages, and API are not designed to do that but can. It also depends on your data. In general with a stateless website, you would want to remember the ID of the data locally and then request it again later rather than storing all of the data (that can be expensive and dangerous). Scoped DI probably isn't the right choice here, but I can't say for certain.

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

      ​@@IAmTimCorey Sorry for the confusion. I have a couple of scenarios with .NET 6 and I'm trying to implement DI properly across both. We use a standard N-Tier solution structure, with BLL, DAL, Models, etc. projects being simple libraries that can be used with Console or Blazor as the "presentation" layer.
      In Blazor/MVC apps, I'm able to set up DI using the builder.service and adding the [Inject] to the component's class file. However, I cannot figure out how to accomplish the same paradigm within the new .NET 6 Console app. The [Inject] annotations do not appear to work (or I have some misunderstandings of how it should work). I understand that we can just instantiate a class using its constructor to pass in dependencies, but I thought there was a way to use the [Inject] annotation to automatically pick up the particular service instance. Maybe I'm wrong on this.
      Also, I understand that we want to instantiate the service once at startup, then pass it around to other solution project classes. Keep it clean and DRY.
      In my case, I want to instantiate the ProtectedBrowserStorage at startup, then pass it to the BLL where I have a repo manage what goes into and out of the session state. But for whatever reason, the session instance is NULL even if passed to a constructor. I found this to be true with a repository instance as well. However, if I instantiate the session and repo and skip the builder.Service, I can pass them into other classes without issue.
      Of course, in all of my attempts, I probably confused myself and got a wire crossed somewhere. Trying to wrap my head around it.
      As you can see, I probably need a concise tutorial on how to get it to work the right way. Love your content! Keep it up.

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

    Tim, in this video, I thought I understood something but then when you ran the code, I guess I must have misunderstood it. I'm not sure if this is relevant but I think it is. I've stopped and grabbed a screen snippet for myself at 40:22 in the video so if you explain this later in the video, my apologies upfront for not watching further. Just before 40:22, you were extremely clear (I thought) that the whole purpose of using logger.LogInformation("Displaying values of {Value1} and {Value2}", logic.Value1, logic.Value2) was so that in the log, we would get a string literal of "Displaying values of {Value1} and {Value2}" followed by a column of the value stored in logic.Value1 followed by a column of the value stored in logic.Value2. However, at 40:22 in the video where I took my screen snippet, you were showing the output window where the logger would log the values and in that window, your logger appears to be showing one literal of "Displaying values of 996 and 851" which IS replacing the {Value1} and {Value2} portions of the log with the actual values stored in the logic fields exposed by those properties and it doesn't show the columns of the logger at all like you had said which is exactly what you had just said that it wouldn't do if we used the syntax that you used. So now I'm confused and I'm not even sure if this has to do with dependency injection (Did a wrong logger get injected or what?) or if it has more to do with logging (which will be a video on my to-do list to watch now)... Could you please, especially in the event that this is due to a wrong logger being injected, explain why yours didn't work (at least the way I understood you saying that it should and would)? It seems like you're moving right along with the video here but maybe that's the whole point and you're about to explain. I'm just not sure and feeling lost now. I love all of your videos and how in-depth you are at explaining this stuff! I'm such a Virgo - I don't just google for snippets that work; I want to understand the code and HOW it works so your under-the-hood explanations are always so welcome in my world. I'm just lost and frustrated on this one as I've rewound and watched your prior discussions twice and what you were saying made perfect sense to me but then wham this output is nothing like what you had said I should expect and nothing like what my code-brain was expecting so please tell me what I'm missing! Thanks as always. You're in my top three youtubers and I can't get enough! (Your Dapper videos the best ever for the record!)

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

      Serilog allows us to capture structured logs. However, our logging sink needs to support those structured logs. The console sink does not support structured logs. However, it still falls back to "normal" logs in those cases. When you then hook up Seq as a sink instead (or along with the Console), you will get your structured logging broken out. This video wasn't focused on Serilog and Seq, though. Rather, it was focused on using dependency injection to swap out your logger. If you want more depth on Serilog, Seq, and structured logging, check out this video: ruclips.net/video/_iryZxv8Rxw/видео.html

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

    Thank you amazing teaching and information.

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

    Trying to convert a Windows Forms app to .NET Core. Changing to use DI as well. Couple of questions. If you have lots of services that a process uses do you need to create a constructor that has many parameter's? Or is there a way inside the component to be able to ask for the service when/if needed?

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

      I had the same question

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

      You need to provide them through the constructor. That's one of the benefits of dependency injection - you declare your dependencies up front instead of burying them in the code. By putting them all up front, you may have a dozen or more dependencies. That seems like a lot in the constructor, but that is just exposing what you are actually doing in the code. If you think that is too many dependencies, you need to clean up your code to not depend on as many things. Reducing your code complexity will be a big one.

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

      @@IAmTimCorey Do you have an example for that?

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

    This is an excellent video. Thanks.

  • @Dimitris.Christoforidis
    @Dimitris.Christoforidis 2 месяца назад

    Thank you❤!! Tim!! I have watched everything for di in youtube and in your site. I am just stacked in a specific situation.
    1. Should i avoid static methods and use singleton instead?
    2. In which case i i need to use singleton and in which transient? I will give you an example on this. I have a class that includes only one method. No variables just one method called GetCustomers(). In this method i making a sql server query for get and return customers. What is the best approach for this class? Singleton, static method or transient??

    • @IAmTimCorey
      @IAmTimCorey  2 месяца назад +1

      There are reasons for static and reasons for singletons. I have videos covering those topics. As for your specific situation, it depends. If the method opens a connection, makes the call, and closes the connection (it is self-contained), then it is probably best as a singleton. If the method stores state of any kind, it might be better as a scoped or even transient.

    • @Dimitris.Christoforidis
      @Dimitris.Christoforidis 2 месяца назад

      @@IAmTimCorey Thank you Tim! what you mean . Please give me an example in which senario do you mean? Do you have videos in your site that covering difference between singleton and trasient and static method in depth? Please give me the link

  • @marcouitendaal985
    @marcouitendaal985 2 месяца назад

    Hi, just enlisted to your .Net intro course. looking forward to it after watching this video and the one on console applications with dependency injection.
    I'm still wondering though about the following: with a .Net template I kind of get it. Every class can get access to the services container and request a service. Good for decoupling. But with your video on the console I got kind of confused. When I instantiate an object from let's say main, that object does not have access to services, right? not even if the console has set up a host.
    my guess is that when you have template code like a Web API project, then the 'usecontrollers' thing in main takes care of making sure that all controllers get access to the host.
    Does your dependency injection course go deeper into this? because for me it's now still 'magic' to see that the classes inside a WebAPI or MVC templated project do have access to the services container, while in the console version I still have to pass the host. (or I'm doing it wrong, that's also very possible)
    anyway, the question is does your dependency injection course go deeper into how the classes get access to the host/services?

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

    You are Tim Corey! Thank you!

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

    Thanks Tim👍
    last week i was in a wishful thinking that your next topic will be something about IoC or DI then this 🤯

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

    Another fantasic job!

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

    Another great tutorial. Looking for advice... I have a validator class using an interface, which more than one methods in the class will validate a single model(DTO/POCO). I planned on passing this model instance in the constructor, but am hitting issues with dependency injection and that it doesn't know about the POCO model class. How should I approach this? I prefer not to pass the model around to every method, was also hoping to avoid using a setter. Using constructor seemed to make sense so that the class has what it needs but doesn't seem to work with DI.

  • @ebrahim-s-ebrahim
    @ebrahim-s-ebrahim 2 года назад

    Thanks a lot. This video was on time.

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

    Wow thanks ALOT.
    This was just what I needed!

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

    Very nice explanation.
    One thing I never heard was that an Interface needs a concrete class to implement it. I know this is not a vid on Interfaces but ... A brief explanation about programming to Interfaces and not classes would go a long way.

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

      I do have a whole video on interfaces: ruclips.net/video/A7qwuFnyIpM/видео.html

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

    hello tim thank you for the content just a suggestion when you explained the logger is good doing shorts on youtube about tips and tricks like that would be great thank you

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

      Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

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

    Excellent video. Thank you very much.

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

    This was seriously perfect, Thanks a lot.

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

    Is it possible to inject a dependency into a class without being forced to use the constructor? Is there a handy way similar to @inject that exist for classes not linked to a view?

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

    Thank you for the video. What about Dependency Injection in .NET Core for WPF?

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

      Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

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

      Use SimpleInjektor

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

    How does the server know when to release memory for the scoped dependencies? My understanding is that http is stateless. Is there a http communication to the server when a tab or browser window is closed?

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

    Well done, Tim.

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

    Tim, I wonder if parameters in constructors are supported. Your examples didn't use contructors with parameters :(

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

      There are ways to do parameters in constructors, but it is really a hack. The ideal solution is to pass data in outside of the constructor.

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

    Thank you Tim. thank you for your efforts. I am not a .NET developer but interested to learn it. Anyway what makes me check your tutorial on dependency injection(DI) is that I've noticed that in many .NET tutorial they intensively use DI, nearly every class they add to the system they add it to DI.
    For me I think using DI can be useful only in places where the class can be used in multiple places, such as the Logger you mentioned in your video. On the other hand adding all models to DI will make the code in the program complected with nearly no need for that as most of the classes will only be used once or tow times.
    What do you think? is it practical to add all models to DI?

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +5

      I don't add models to DI normally. Those don't create dependencies. However, all other classes get added to DI all the time. There are very few apps where I don't use DI. It allows for easier/better upgrades, less coupling, and easier testing. Here is a video on the Dependency Inversion Principle (the D in SOLID) that explains this concept better (DI is an implementation of this principle - they aren't the same, which I will explain in the video): ruclips.net/video/NnZZMkwI6KI/видео.html

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

    Thanks a lot for this valuable explanaiton. in case you have a new class, also, you will create an interface to it to register it, so, the place you want to consume it, you have to change the interface injection to the new interface, please correct me if I'm wrong. Also, I have inheritance "Abstract Class" with some common implementation for subclasses, using DI with inheritance is impossible, because they are different concepts. I tried it a lot, I didn't succeed. Appreciate if you correct me if I'm wrong. Thanks

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

    Really good tutorial, thanks.

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

    thank you for your videos! great help!

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

    My basic project to save Xlsx ko Xlsm works fine in my computer but it shows error and crashes at "saves as" line in others computer.
    I used Ms excel interop for it.
    What do u think would be a reason for error?

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

      I was able to pin point error upto "save as" Line. But I don't know how I can resolve the issue.
      My. Computer has 2019 office and all. Computer I tested had older version.

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

      That's probably the issue. Excel Interop uses the version of Excel installed on the computer. That's the downside of using a library like that - you need to have Excel installed in order for it to work.

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

    I cannot wrap my head around any scenario where switching from logic to betterLogic is the case. Why not just change the classes? This comes from absence of experience tho. Is this when you have different projects which take data from project A to project B and project C has the interfaces?

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

    Excellent timming

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

    Tim, if I am trying to make a list of fake employees to populate a demo application with, is it bad practice to add scoped dependency injection with a DemoDatabase class? (interfaced of course).
    I want the user to have the ability to CRUD employees and have it reset when they leave the application. But I don't want that data to persist anywhere and I don't need an actual database - it is just so IRL employers can test my application in browser and assess its quality.
    Thank you for your great tutorials and for taking your time to respond to commenters - I have seen you recently reply to people on videos from years ago! We appreciate the dedication!

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

      That seems like a fine solution. As long as you understand the dangers (and you do), and you know that it is a demo and not something you should do for a "real" production app, go ahead and implement it.

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

    you explained perfectly

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

    Fantastic video! Now I need to go watch the full logging video. However, what if I wanted to inject into a class, which video covers that?

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

      Inject logging or just any class? If you want to inject any class, just put that class in the constructor. If you want to inject a logger, put "Logger log" as a parameter in your constructor.

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

      @@IAmTimCorey Thank you! I'm currently watching your appsettings.json video, and there is a small snippet in there where you show this! I've wasted hours and hours messing around with code trying to figure it out, you showed it in about 20 seconds. I love it!

  • @pcasalinho
    @pcasalinho 3 месяца назад

    Hi! I'm following this example in as a Blazor Web App .net8 template in vstudio 2022 17.10.5 with Server render mode (per page/component). I can't get the AddScoped to work, as it always behaves like Transient. Singleton and Transient work as expected but Scoped always instantiates a new object every time. What's the diference?
    **edit:** `@rendermode @(new InteractiveServerRenderMode(prerender: false))` makes it behave like expected but not sure why?

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

    You're the best. You know that, right?

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

    would you please make a video explaining to us "the real benefits" of "struct/abstract/.... extra" or they are there abandoned and they have no real usage in our daily usage in programming

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

      Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

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

    if i wanted to preload a singleton (eg load some info from DB one time on first access) how do i achieve that with DI?

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

    Tim,
    Thank you that make it understanle.
    Q: in case i have Load Balancing for the web server, how exactlly it will affact the Singleton process?
    Will the singelton instance be created once? or several times per each server instance?
    Unfortunaterlly, I don't have the option to implament it and checking live by myself.
    I just know that in the very old past, the answer was object instance per server instance.
    Is it different in .net 6?

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

      It would be once per server. In a load balancing situation, each instance only knows about itself. If only one of the server instances initialized the singleton, the rest wouldn't have it to use.

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

    So if I have the service in one project and the "consumer" in another, I necessarily need a third project, to put the interface in, and have both of the first two projects referecing it, right ? Just thinking where to put the interface, when organizing projects.

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

      You could. That is the typical way of doing it.

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

    Hey Tim,
    Thanks for the excellent explanation. I've watched your DI in WPF but I am still left with a lot of questions. Should I include all views and view models as dependencies? Can my view models have models as constructor arguments? Is it best to keep all constructor arguments of type interfaces?

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

      Views? No. ViewModels? Yes. ViewModels are classes and they have dependencies that need to be fulfilled by the DI container. As for constructor arguments, technically you can but the ideal situation is to avoid that and pass the value in after instantiation.

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

      @@IAmTimCorey I guess I thought views are registered because in your WPF video you had registered that main window and child forms. Was that only for demonstration purposes?

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

    Thank you for the awesome video.

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

    Thanks!

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

    Nice Explanation
    Thanks

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

    Great video!

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

    one probelm I ran into was having two concrete implementations that shared a interface that I needed in different situations. do you have a good solution to this issue?

    • @aah134-K
      @aah134-K 2 года назад

      Yes easy with multiple ways,
      One way is reguster the concrete class for both and request the one cobcrete or both as you need.
      Another yoy can reciwcw both objects and select the one you like with linq query

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

      49:40

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

      Yep, I addressed that in the video.

    • @aah134-K
      @aah134-K 2 года назад +1

      @@Holyflare it wasnt mwntioned how to allow both, he said change the interface and not doing this way.
      Sometime the di depend on dynamic runtime and both can be requested.
      The way I did it, do
      Services.AddTransient();
      Services.AddTransient();
      Then in ctor used the Concrete signature to get the one you like

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

      I did say you could do what you are suggesting. I also said you could put both in and then get an array of items. What you are doing is creating a hard dependency, which reduces the benefits of dependency injection. Why not just create two different interfaces, if you really want two different class instance types?

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

    Ahhhhh thanks Tim!

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

    Fantastic, Tim! I was recently tasked with creating a new .NET 6 API project and this video gave me a lot of "ahaaa!" moments.
    You had mentioned AutoFac, but just out of curiosity, do you ever cover Scrutor?

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

    27:12 MAUI use scoped per window IIRC. Also, you can create a scope manually.. Nested scopes doesn't work unfortunately. I did some experiments.

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

    @49:59 in Spring Boot we can easily get different beans for same interface just by mentioning the bean name or id during creation and wiring the bean with the same name to the target variable. Most of the things are better in Spring Boot except for few things like short hand syntaxes, build size and memory consumption which are better in .net.

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

      Mentioning the bean name is the issue, though. Then you have coupling. That's something you need to be careful of. If you cannot swap out an element without changing your underlying code, you should probably rethink how you are doing dependency injection. Otherwise, you are basically creating distributed tight coupling.

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

      Hm... you are also correct at your end, but in dotnet as well if you want to change a dependency you will to change in code i.e. in Program.cs file. In Spring Boot dependencies are created automagically using @Component or related stereotype annotations. These dependencies can be injected using constructor. This saves lots of developer's effort. In dotnet you will have to manually inject the dependency from Program.cs everytime you create the dependency class. In Spring Boot as well we can keep all the dependency at one java config file using @Bean annotation above methods returning bean. But it is not followed much for services and controllers because whole point is that we should be able to change the implementation of dependency by changing one line of code, which can still be done by removing @Component annotations from the previous dependency classes and adding in required ones.

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

    How does .NET know that builder.Host.UseSerilog overrides the default logger. Does Serilog impliment a needed interface of that is the trigger? Great video!

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

      When you set up Serilog in Program.cs, you put it in Dependency Injection. That overrides the default ILogger instance (Serilog implements ILogger).

  • @User-dh8ot
    @User-dh8ot 2 года назад

    Is it really necessary to always extract an interface for every class you need for DI? Because the interface is just used for that one implementation. Is this not against the concept of YAGNI? I mean why not injecting the concrete class? Whats the disadvantage?

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

      By using an interface, we can easily apply unit testing to our project. Interfaces allow us to easily mock dependencies.

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

    Hi Tim. Thank you for help that you give with videos. I would really like to see di in winforms.

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

      Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

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

    Thanks for the video! Love DI. But I don’t know how to do it in a console app. Couldn’t find where I can leave a suggestion on your site.

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

      Check this Tim's course. It could be helpful for what you need.
      ".NET Core Console App with Dependency Injection, Logging, and Settings"
      ruclips.net/video/GAOCe-2nXqc/видео.html

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

      ruclips.net/video/GAOCe-2nXqc/видео.html

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

      Go to suggestions.IAmTimCorey.com

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

    Hey Tim, do you ever do webinars for companies, if not, would you ever be interested?

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

      Very rarely. It depends on what the company needs, how much prep work it would take on my part, and if I have time in the schedule to make it happen.

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

    Now i understand how to inject a service in razor or .cs-code-behind file. But how to inject it in a class, in which a complex businesslogic is encapsulated for background work (located in an own project) ?

    • @IAmTimCorey
      @IAmTimCorey  11 месяцев назад +1

      The same way. Your UI layer handles the dependency injection setup, regardless of what that UI layer is. In the case of a background service, the UI is the project that starts and stops the application.

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

      @@IAmTimCorey Thanks for your answer and your great videos, but I haven't quite understood it yet:
      Is the dependency passed to the backgroundjob via the constructor (constructor injection)?
      Or can this dependency also be injected via a corresponding keyword similar to @inject? My problem is that I cannot use @inject in a normal cs class.

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

    Why I can't have a model like a LIst as a scoped dependency injection so I can work with this list at different pages? It is not a database synced list, it just my current selection (probably not even a user at this time)

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

      You technically can and for demo purposes (I just verified that it works), that would work, but it isn't the right solution for models. Especially since you usually want to overwrite the List instance, which is not something you should do with dependencies from DI. Models don't need to come from DI because they aren't dependencies, just data.

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

    35:07 How would you use this .NET Core structured Logging in combination with a StringBuilder? Let's say we have a List and want to display the IDs of all objects where a specific property is false for example. In this case I currently have a StringBuilder, iterate through the List, append to the builder whenever a condition is true (currently with append() and string-interpolation), and then write the result into the log file just once. Should I stay with that approach or rather explicitly call logger.LogWarning() or logger.LogDebug() for every affected object?

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

      It depends. If you just want a string and don't want to filter or query on any of the data in that string, StringBuilder is fine. Otherwise, you should consider another route. You could convert the list to JSON and pass that in as a parameter of your log statement. Or you could log each line.

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

    Hey! Nice video and explanation! I'm afraid I understood just a small part of the video content, due the fact that I'm begginer in .NET development. So, I'd like to know if you could recommend other videos about this topic that might be a level below this one. Also, which concepts are import to have really clear in order to understand the video content? Maybe interfaces... what else? Thank you!

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

      Understanding interfaces will be of help ( ruclips.net/video/A7qwuFnyIpM/видео.html ). Maybe understanding the changes that came about with .NET 6 as well might help ( ruclips.net/video/vdhFw1VSowg/видео.html ). It really does depend on where you are at. This video is more generic on what to learn in C# when: ruclips.net/video/hnjly9iBHso/видео.html

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

      @@IAmTimCorey Thanks for the reply

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

      @@IAmTimCorey Just watched the interfaces videos. Amazing tutorial.. I had a basic knowledge on interfaces and now I know a bit more about them... I've finished watching the video knowing that I've learned 3 or 4 new things. Nice!

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

    Thank you Tim

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

    Hi Tim, I'm facing a problem in MySql. User 'a8b531_cmsp1' has exceeded the 'max_user_connections'. How can solve this?

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

      It sounds like you are not properly closing your connections to the database at the end of the call. Make sure to close and dispose of the connection.

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

    What happens in the situation when you want to swap your normal logger with a 3rd party logger? But this 3rd party logger uses a different function to log, for example, it uses Log(string text) instead of the one used in the video LogInformation(string text). You still end up changing everywhere in your app because of this.
    Basically what I am saying is that if you want to swap with something else but it does not implement the same interface, what do you do?

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

      Most third party loggers (or he main ones) implement the ILogger interface. Those other methods are just wrappers on the Log method.

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

    Can u please please make a video for dependency injection in azure functions??

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

      Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

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

    How to you share exact needed tutorial whenever I need? You're amazing! Thank you for teaching!

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

      I am glad it was so helpful.