Dependency Injection in .NET Core (.NET 6)

  Рет қаралды 195,122

IAmTimCorey

IAmTimCorey

Күн бұрын

Пікірлер: 307
@troyfulton5542
@troyfulton5542 Жыл бұрын
Thanks!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@themateoo
@themateoo Жыл бұрын
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.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@attilaguba856
@attilaguba856 Жыл бұрын
Totally agree with you!
@snahhhhhhh
@snahhhhhhh 2 ай бұрын
Tim is great
@SergeiFedorov77
@SergeiFedorov77 2 жыл бұрын
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.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@BrunoTorres05
@BrunoTorres05 2 жыл бұрын
what a timing! that's exactly what I was looking for, thank you so much.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@Luke-me9qe
@Luke-me9qe 2 жыл бұрын
@IAmTimCorey and you, kind sir, are heaven sent
@MaxenGaynier28
@MaxenGaynier28 2 жыл бұрын
Same!
@kailas030
@kailas030 Жыл бұрын
So called complex topic explained in a super simple manner. You're awesome
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@randycasey49
@randycasey49 2 жыл бұрын
I vote for video to demostrate how to setup dependency injection for console app. It would round-out this discussion. Thanks!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@BonnieMan42
@BonnieMan42 2 жыл бұрын
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 жыл бұрын
I am glad it was so helpful.
@jilmehta9397
@jilmehta9397 2 жыл бұрын
bestest free course I am ever watching. Thank you very much
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@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.
@alisaadat8638
@alisaadat8638 2 жыл бұрын
Tank you Tim it was the best Dependency injection course i've ever seen.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@AdityaPratapSingh002
@AdityaPratapSingh002 2 жыл бұрын
Best video on DI in .Net 6.0. It helped me to understand DI in better way.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I am glad it was so helpful.
@dirk8860
@dirk8860 2 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Glad it was helpful!
@mcintoshdev
@mcintoshdev 2 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@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();
@jesusdelarua5995
@jesusdelarua5995 2 жыл бұрын
Thank you Tim. Your videos are very useful. I really appreciate your efforts in helping us.
@IAmTimCorey
@IAmTimCorey 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
@sethamballa9428
@sethamballa9428 Жыл бұрын
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
@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.
@swedishprogrammer
@swedishprogrammer 2 жыл бұрын
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 жыл бұрын
Do you mean beyond what I covered here? kzbin.info/www/bejne/eqjFiquGYsqng7s 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 Жыл бұрын
.
@hanitalaat5739
@hanitalaat5739 2 жыл бұрын
That's what am looking for loooong time . special thanx for you Tim Corey
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@irocx8745
@irocx8745 2 жыл бұрын
No words are enough to say how great info this video is. Just gratitude 💯💯💯💯
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Glad it was helpful!
@ahmedelgendy5363
@ahmedelgendy5363 2 жыл бұрын
Very Clear Description for DI Thanks TIM
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@sudhanandabiswas7415
@sudhanandabiswas7415 2 жыл бұрын
Thanks for a great videos. This is really filled a lot of topics I missed on Dependency injection.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@attilaguba856
@attilaguba856 Жыл бұрын
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 Жыл бұрын
Glad it helped!
@attilaguba856
@attilaguba856 Жыл бұрын
@@IAmTimCorey absolutely helped! Thanks! Going to check your courses!
@torrvic1156
@torrvic1156 Жыл бұрын
Outstanding explanations! As usual.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@torrvic1156
@torrvic1156 Жыл бұрын
@@IAmTimCorey no! THANK YOU TIM!
@webluke
@webluke 2 жыл бұрын
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.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks!
@geoffreyhibon2651
@geoffreyhibon2651 Жыл бұрын
Thanks a lot for this very good presentation Tim. We need to stay focus regarding the number of interesting point provided ;)!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@zonimaru
@zonimaru 2 жыл бұрын
You just saved my mind from a lot of frustration, thank you.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Glad I could help!
@monat_son
@monat_son Жыл бұрын
Thank you sir, you are my guy for C#! Thanks for sharing the knowledge!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@hamzaanushath
@hamzaanushath Жыл бұрын
Mind-blowing explanation, every single second of this video is information information and information ⭐⭐⭐⭐⭐
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Glad you liked it!
@hamzaanushath
@hamzaanushath Жыл бұрын
@@IAmTimCorey not only that, watching many of your videos related to C#. 👑One of a kind👑
@KouroushMetal
@KouroushMetal 2 жыл бұрын
very well info on Scoped, that help me understand it better! Thank You!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@navid-wu6bx
@navid-wu6bx Жыл бұрын
Dear Tim, your video helps me a lot, you'r incredible!!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Happy to hear that!
@jjque322
@jjque322 Жыл бұрын
Best explanation on web!!!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@manuelgamezz
@manuelgamezz 2 жыл бұрын
Excelent content about DI in net core, Thanks Tim for sharing this information.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@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!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@BloodHaZaRd666
@BloodHaZaRd666 2 жыл бұрын
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 жыл бұрын
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/
@VinnayKulkarni
@VinnayKulkarni 2 жыл бұрын
Thanks Tim for the wonderful content, easy to understand
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@alexwexov4298
@alexwexov4298 2 жыл бұрын
Great video, looking forward for more .NET > 6 !
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks!
@ahmaddawood9691
@ahmaddawood9691 2 жыл бұрын
Really Excellent tutorial .. thanks for your effort
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@macanthony1985
@macanthony1985 Жыл бұрын
You are Tim Corey! Thank you!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@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: kzbin.info/www/bejne/lZrVqo2uq52Frtk
@simplepycodes
@simplepycodes 2 жыл бұрын
This was seriously perfect, Thanks a lot.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@BryanPollardPhotos
@BryanPollardPhotos Жыл бұрын
Another fantasic job!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thanks!
@DukaSoft
@DukaSoft 2 жыл бұрын
Wow thanks ALOT. This was just what I needed!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You're welcome!
@ebrahim-s-ebrahim
@ebrahim-s-ebrahim 2 жыл бұрын
Thanks a lot. This video was on time.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@sonofvideo4696
@sonofvideo4696 Жыл бұрын
This is an excellent video. Thanks.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@pardhasaradhivasamsetty7693
@pardhasaradhivasamsetty7693 Жыл бұрын
Thank you amazing teaching and information.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are very welcome.
@sajadseniordeveloper4198
@sajadseniordeveloper4198 2 жыл бұрын
Well done, Tim.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you.
@jimmevandermeer6511
@jimmevandermeer6511 2 жыл бұрын
you explained perfectly
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you!
@sahild6584
@sahild6584 2 жыл бұрын
Excellent timming
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks!
@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 🤯
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Awesome!
@OlegTserluk
@OlegTserluk 2 жыл бұрын
Excellent video. Thank you very much.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@szilviemailje
@szilviemailje 2 жыл бұрын
thank you for your videos! great help!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@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.
@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.
@obeydullahsobratee
@obeydullahsobratee 2 жыл бұрын
Really good tutorial, thanks.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@mohamaddjelouah9332
@mohamaddjelouah9332 2 жыл бұрын
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 жыл бұрын
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/
@RajVadla
@RajVadla 2 жыл бұрын
Thank you Tim
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@realbitt
@realbitt Жыл бұрын
Great video!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thanks!
@vigneshganapathy6359
@vigneshganapathy6359 2 жыл бұрын
Nice Explanation Thanks
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@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: kzbin.info/www/bejne/d2jUqKh8o958pq8
@glennmayer3862
@glennmayer3862 2 жыл бұрын
Ahhhhh thanks Tim!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@dilipjain9581
@dilipjain9581 2 жыл бұрын
Thank you for the awesome video.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@AvgDan
@AvgDan 2 жыл бұрын
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.
@kewlpackstudios
@kewlpackstudios 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 жыл бұрын
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 KZbin 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.
@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/
@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.
@joseluizdurigon8893
@joseluizdurigon8893 Жыл бұрын
You're the best. You know that, right?
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@AkshaySrinivasan123
@AkshaySrinivasan123 2 жыл бұрын
Very nice!! Thank you!!!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@Dimitris.Christoforidis
@Dimitris.Christoforidis 3 ай бұрын
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 3 ай бұрын
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 3 ай бұрын
@@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
@imadabab
@imadabab Жыл бұрын
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
@mrsajjad30
@mrsajjad30 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
@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.
@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?
@keithfranklin214
@keithfranklin214 2 жыл бұрын
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?
@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.
@robertkerr229
@robertkerr229 2 жыл бұрын
What is being said at 5:06, "Can still use ada Config"?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
EditorConfig.
@baseljuma158
@baseljuma158 2 жыл бұрын
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 жыл бұрын
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): kzbin.info/www/bejne/hJ-9i4ChrK5pgas
@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!
@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 жыл бұрын
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.
@CodeguruX
@CodeguruX 2 жыл бұрын
I'll say what everyone else is thinking. Thanks for comprehensive topics, without an Indian accent that hurts my brain. There is way too much noise out there cluttering up coding tutorials and almost none of them help.
@synthopia2
@synthopia2 9 ай бұрын
All those tutorials with english title but uses hindi language 💀 What I'd not give to block all indian content in youtube
@a_n_m3516
@a_n_m3516 3 күн бұрын
White racism
@kitsurubami
@kitsurubami 2 жыл бұрын
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?
@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?
@MrOneWorld123
@MrOneWorld123 2 жыл бұрын
Tim, I wonder if parameters in constructors are supported. Your examples didn't use contructors with parameters :(
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
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.
@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?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I haven’t yet.
@milanpaudel9624
@milanpaudel9624 2 жыл бұрын
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.
@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.
@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 Жыл бұрын
Understanding interfaces will be of help ( kzbin.info/www/bejne/d2jUqKh8o958pq8 ). Maybe understanding the changes that came about with .NET 6 as well might help ( kzbin.info/www/bejne/rJXLd6pni7iirck ). It really does depend on where you are at. This video is more generic on what to learn in C# when: kzbin.info/www/bejne/np_Nnaxvnqd7qdE
@rodrigomoro8047
@rodrigomoro8047 Жыл бұрын
@@IAmTimCorey Thanks for the reply
@rodrigomoro8047
@rodrigomoro8047 Жыл бұрын
@@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!
@MrOneWorld123
@MrOneWorld123 2 жыл бұрын
43:31 Courses about appsettings: kzbin.infosearch?query=appsettings
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Here is the course on AppSettings: www.iamtimcorey.com/p/net-core-appsettings-in-depth
@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?
@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.
@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?
@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?
@DoctorMGL
@DoctorMGL 2 жыл бұрын
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/
@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?
@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" kzbin.info/www/bejne/fXKydJhjZ9OLp8U
@mrsajjad30
@mrsajjad30 2 жыл бұрын
kzbin.info/www/bejne/fXKydJhjZ9OLp8U
@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.
@mana20
@mana20 2 жыл бұрын
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 жыл бұрын
49:40
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Yep, I addressed that in the video.
@aah134-K
@aah134-K 2 жыл бұрын
@@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 жыл бұрын
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?
@theroboticscodedepot7736
@theroboticscodedepot7736 Жыл бұрын
That was all quite beautiful and clearly explained but you could have just said DI is like Delegates for class constructors. Admittedly terse but concise.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
But that's not what DI is, which is why I didn't say that.
@theroboticscodedepot7736
@theroboticscodedepot7736 Жыл бұрын
@@IAmTimCorey WRONG! It is EXACTLY what DI is!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
First, DI doesn’t have to use constructors. It could use property injection or even be called directly when needed (not usually ideal but possible). Second, a delegate is a method that is passed around like a variable that can execute code in the original location when called from the passed location. DI creates and passes class instances around. They execute where they are passed, not in the DI container. If you would like to brush up on what delegates are, I have a video on them.
@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).
@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.
Factory Pattern in C# with Dependency Injection
1:07:33
IAmTimCorey
Рет қаралды 110 М.
C# Dependency Injection with Autofac
54:45
IAmTimCorey
Рет қаралды 297 М.
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 101 МЛН
كم بصير عمركم عام ٢٠٢٥😍 #shorts #hasanandnour
00:27
hasan and nour shorts
Рет қаралды 12 МЛН
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
Dependency Injection Deep Dive | .NET & C# Essentials
38:01
Amichai Mantinband
Рет қаралды 27 М.
Structuring Dependency Injection In ASP.NET Core The Right Way
16:22
Milan Jovanović
Рет қаралды 54 М.
The Logging Everyone Should Be Using in .NET
15:34
Nick Chapsas
Рет қаралды 86 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 890 М.
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 101 МЛН