Dependency Injection for ASP NET Core 6.0 - An UPDATED 2022 Tutorial

Поділитися
Вставка
  • Опубліковано 23 лип 2024
  • 🚀 C# Progress Academy - Become a senior C# developer: academy.tutorials.eu/p/csharp...
    Dependency Injection in ASP.NET Core 6.0 has been one of our number 1 requests, and now it´s finally here! Want to learn more about C# and ASP.NET?
    Get to work with Dependency Injection in an ASP.NET 6 MVC Project!
    Here are some links that you may find interesting👀
    · Check out the C# Masterclass: bit.ly/38xC6WW
    · FREE C# Download 9 MUST have .NET Developer tools: page.tutorials.eu/tools
    We´ll make sure to make a Developer out of you In no time!
    ☕ You can also support us now by buying us a coffee! ☕ www.buymeacoffee.com/tutorialsEU
    And we promise to create more cool content for you!
    ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies.
    For more information specific to dependency injection within MVC controllers, see Dependency injection into controllers in ASP.NET Core.
    For information on using dependency injection in applications other than web apps, see Dependency injection in .NET.
    For more information on dependency injection of options, see Options pattern in ASP.NET Core.
    This topic provides information on dependency injection in ASP.NET Core. The primary documentation on using dependency injection is contained in Dependency injection in .NET.
    View or download sample code (how to download)
    Overview of dependency injection
    A dependency is an object that another object depends on. Examine the following MyDependency class with a WriteMessage method that other classes depend on.
    A class can create an instance of the MyDependency class to make use of its WriteMessage method. In the following example, the MyDependency class is a dependency of the IndexModel class.
    The class creates and directly depends on the MyDependency class. Code dependencies, such as in the previous example, are problematic and should be avoided for the following reasons:
    · To replace MyDependency with a different implementation, the IndexModel class must be modified.
    · If MyDependency has dependencies, they must also be configured by the IndexModel class. In a large project with multiple classes depending on MyDependency, the configuration code becomes scattered across the app.
    · This implementation is difficult to unit test.
    Dependency injection addresses these problems through:
    · The use of an interface or base class to abstract the dependency implementation.
    · Registration of the dependency in a service container. ASP.NET Core provides a built-in service container, IServiceProvider. Services are typically registered in the app's Program.cs file.
    · Injection of the service into the constructor of the class where it's used. The framework takes on the responsibility of creating an instance of the dependency and disposing of it when it's no longer needed.
    Interested in this? Watch the video to find out more about ASP NET Dependency Injection!
    #asp #dotnet #DependencyInjection #code #programming #csharp
    TIMESTAMPS
    00:00 Dependency Injection in .NET? Yes please!
    00:24 Why dependency Injection?
    01:00 Creating a Service
    02:32 Register our Service
    04:52 Using our Service
    07:20 Summary on how to get the DI going
    07:58 Thanks for watching!
    TAGS
    dependency injection in asp.net core,asp.net dependency injection,dependency injection,visual studio,c# dependency injection tutorial,dotnet,dependency injection explained,dependency injection tutorial,csharp,c sharp,di,ioc,tutorial,programming,code,visual studio 2022,asp.net core 6,dotnet 6,asp.net core api,.net core,asp.net core,tutorialseu,asp net,mvc,asp net core,tim corey,asp net mvc,dotnet core,asp net core mvc,freecodecamp,dev,iamtimcorey,how to
    tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
    This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
    Stay tuned and subscribe to tutorialsEU: goo.gl/rBFh3x
    Android: / @tutorialseuandroid
    C#: / @tutorialseuc
    Unity: / @tutorialseuunity
    Facebook: / tutorialseu-1093802040...
    LinkedIn: / tutorialseu
    Discord: / discord

КОМЕНТАРІ • 106

  • @tutorialsEUC
    @tutorialsEUC  2 роки тому +3

    🚀C# Progress Academy - Become a senior C# developer: academy.tutorials.eu/p/csharp-progress-academy

  • @jimmuelagwit7032
    @jimmuelagwit7032 Рік тому +24

    Kind sir, you made a one-hour video tutorial in just 8 minutes.. Thank you.

  • @jasonhewko7639
    @jasonhewko7639 2 роки тому +14

    This is the most clear and concise explanation of implementing a service through dependency injection I have seen!

  • @king_and_country
    @king_and_country Рік тому +3

    Of all the DI tutorials I watched, this one finally made it make sense. Thank you!

  • @Jujukungfu
    @Jujukungfu 11 місяців тому +1

    You've helped me learn something I've been stuck on for a WEEK. THANK YOU!!!

  • @BURN1902
    @BURN1902 5 місяців тому +1

    Hondestly, after reading 3 entire Days of documentation, this tutorial did the job in 8 minutes...
    Great one!

  • @ayoubdkhissi
    @ayoubdkhissi 2 роки тому +4

    very simple and clear! thanks dude!

  • @michaelduren8881
    @michaelduren8881 Рік тому +1

    Awesome explanation. I've been trying to get my head around this concept and this finally did it for me thank you!

  • @totoden1987
    @totoden1987 Рік тому +5

    This tutorial is very clear and understandable even for a newbie like me! Thank you very much!

  • @Dearelephant
    @Dearelephant Рік тому +4

    Short and sweet but full of important information. Thank you for the video!

  • @cumbeldore420
    @cumbeldore420 2 роки тому +5

    The first video that has actually helped me understand this concept!! Thank you

  • @vnansen
    @vnansen Рік тому +2

    Well explained in a very short time. Thanks

  • @ayam_jantan_
    @ayam_jantan_ 6 місяців тому +1

    this is very good, short and simple to understand.

  • @olegtishechkin5745
    @olegtishechkin5745 Рік тому +1

    Short and clear, thank you.

  • @nnaemekachukwukezie503
    @nnaemekachukwukezie503 2 роки тому +1

    Very simplified. I love this

  • @heenapatel1728
    @heenapatel1728 4 місяці тому

    Awesome content .. loved the explanation!

  • @sonamawasthi8983
    @sonamawasthi8983 5 місяців тому

    Very well explained thank you so much

  • @prasadmadushan
    @prasadmadushan Рік тому +1

    Man you are amazing, explained those concept in real quick. I would say, every single word is really important to me hope everyone else feel the same.

  • @claudiolacerda7626
    @claudiolacerda7626 6 місяців тому

    Thanks for class!!!

  • @toprope98
    @toprope98 5 місяців тому

    This was an excellent and simple way to understand how to create a service and implement it from DI. Thank you very much!

  • @ozhoneybadger
    @ozhoneybadger 2 місяці тому

    Cheers mate nice and concise

  • @luckythakur8995
    @luckythakur8995 Рік тому +1

    Thank you for keeping things short and precise

  • @dozieasowa6971
    @dozieasowa6971 Рік тому +1

    Fantastic!

  • @jaymitpatel8241
    @jaymitpatel8241 Рік тому

    Perfect, simple and concise video. Thanks!

  • @nothing1715
    @nothing1715 6 місяців тому

    That's what I was looking for , for a long time...I was struggling to understand....Thanks buddy...you are geeat😊

  • @zyadabdullah7268
    @zyadabdullah7268 Рік тому

    This is the first video I watch regarding Dependency Injection, and it’s really clear and helpful.

  • @sergiosepulveda7860
    @sergiosepulveda7860 Рік тому +1

    Nice, simple and precise, thank you!

  • @nooraldeenali4628
    @nooraldeenali4628 Рік тому

    Really nice and easy to understand what the dependency injection is.

  • @albertcjr1639
    @albertcjr1639 8 місяців тому

    great tutorial, concise and clear! keep sharing!

  • @user-tl6em2df9q
    @user-tl6em2df9q 3 місяці тому

    very good explanation of a complex topic. thx

  • @badr6896
    @badr6896 Рік тому +2

    This is the clearest video i have seen in my life. Thanks

  • @786richu
    @786richu 5 місяців тому

    Amazing presentation. I understand this concept in 8 min rather than reading the same in the whole day . Keep posting this type of critical videos and we will definitely watch you❤

  • @huzaifayunus9243
    @huzaifayunus9243 2 роки тому +1

    You are genius. Simple and easy to understand. Please work on delegates in c#

  • @shivankitss8396
    @shivankitss8396 4 місяці тому

    Thanks a ton 🍻

  • @srinidhis9684
    @srinidhis9684 2 роки тому +1

    thank you it is really helpful

  • @geoffreywadsworth7400
    @geoffreywadsworth7400 Рік тому +1

    Solid and to the point. Thanks.

  • @shvetsjr
    @shvetsjr Рік тому +1

    You are awesome! Very helpful, thank you

  • @somehappyguy6598
    @somehappyguy6598 Рік тому +1

    Beautiful! I am now a subscriber.

  • @learner8084
    @learner8084 Рік тому +1

    thanks, This is very good. I need all the information exactly what i needed.

  • @alexmelamud9750
    @alexmelamud9750 Рік тому +1

    Good explanation. Thanks

  • @ldskmain
    @ldskmain 2 роки тому

    Thank You!

  • @Rob-co6iz
    @Rob-co6iz 20 днів тому

    Thanks!!

  • @Ahmet-nd5ct
    @Ahmet-nd5ct Рік тому +1

    greatly helpful

  • @johnwdsouza1
    @johnwdsouza1 Рік тому +1

    Thanks

  • @iroshanaravishan1388
    @iroshanaravishan1388 2 роки тому +1

    Thanks😃🙌

  • @dhliu9687
    @dhliu9687 Рік тому +1

    Hi bro, your explanation is really simple and clear. Also, practically show how to do it. Appreciate!🎉

  • @umarawan4142
    @umarawan4142 15 днів тому

    superb

  • @sergeymigel4680
    @sergeymigel4680 10 місяців тому

    thanks!

  • @Didier-cu6cb
    @Didier-cu6cb Рік тому +1

    thanks

  • @jae3547
    @jae3547 Рік тому +1

    Nice job, if possible do a side by side comparison with Dependency Injection in Blazor Server App vs. Blazor Web Assembly App. Ty!

  • @StevenWong1995
    @StevenWong1995 8 місяців тому

    Good video. I'd love to see you include some of the extra info like advising people to use some sorts of automated service registration tools like AutoFac as this will save their time and hassle as the application grows with more services to be registered line by line.

  • @ScopedMach1ne
    @ScopedMach1ne 2 роки тому +1

    super good video! One thing I am trying to figure out right now. I get that we make the interface and add the interface to the "Dependancy injection system" But when we go back to the controller..... we set a variable [interfaceType _Name] then in the constructor we pass in [interfaceType Name].... Now when I step though the code I See that it's getting the info from the Dependancy injection I just don't understand how? I am missing how passing it into the constructor connects to the dependancy injection register thing. hopefully that made sense lol.

  • @GameSpreePH
    @GameSpreePH Рік тому

    D best. SImple and clear

  • @JanKowalski-ld4ec
    @JanKowalski-ld4ec Рік тому

    Wow thanks for simple explanation what actually the DI is for. Would love to watch email sender implementation or another topic: how to perform lets say monthly activities i.e. e-mail sending. Regards!

  • @attilaguba856
    @attilaguba856 2 роки тому +1

    Great explanation! Thumbs up ! Request fir a tutorial ! A complete Register and Login with Forgot and Reset password ! / Role based authorization! / would be much appreciated! 😊

  • @aminechiki237
    @aminechiki237 Рік тому +1

    Thankk

  • @osmantas369
    @osmantas369 Рік тому +1

    Thank you, short and clear. Btw would you make videos about clean architecture and mediatr as well?

  • @K9NNeo1191
    @K9NNeo1191 Рік тому +1

    Is it possible to make multiple service for one controller?

  • @kitsurubami
    @kitsurubami Рік тому +1

    This video is great very helpful. One thing I need to understand better is the concept of services and containers. Less about the code and the implementation in .net and more about conceptually what a service is, what a container is. I'm aware that it's all for the purpose of dependency injection, and I understand dependency injection, but services, containers, and app building are foreign concepts for me.

    • @tutorialsEUC
      @tutorialsEUC  Рік тому

      Thanks a lot for this feedback! I will put that on our todo-list so that we can create a video regarding services and containers in asp net :)

  • @imadabab
    @imadabab 7 місяців тому

    Thanks for sharing. But all the examples on UA-cam are without existing an abstract class having method implemented, also if the service have properties.

  • @nouralhudamail
    @nouralhudamail 7 місяців тому

    Peace upon you , I have start learning .core .6 with c# and trying to do comment with rating and admin dashboard from scratch so since you mentiond to comment on the video about something to make video about it I droped this comment ...

  • @igboanugwocollins4452
    @igboanugwocollins4452 Рік тому

    Please i have been looking for this in nestjs, does anyone know of an approach

  • @idowuolayinka9145
    @idowuolayinka9145 Рік тому +1

    Can someone help out with the namespace to be implemented in the program class? Take for instance like the IEmailService he used. What would be the namespace in the program class?

    • @stephanieezat-panah7750
      @stephanieezat-panah7750 11 місяців тому +1

      I believe that all files beneath a project in a solution, that that project name as their namespace

  • @ferdaousharmel704
    @ferdaousharmel704 2 роки тому +2

    Nice and simple ! please I have a question there.Do we really need to use Autofac in .Net 6 ? if yes/no would you please explain why ? thank you for your reply.

    • @tutorialsEUC
      @tutorialsEUC  2 роки тому

      Hi, there! Glad you like it.
      This is a questions, that's not so simple to answer with yes or no. Take a look at this autofac migration documentation:
      autofac.readthedocs.io/en/latest/integration/aspnetcore.html
      Cheers :)

  • @Buzz-pc4ep
    @Buzz-pc4ep 6 днів тому

    Please make a video for make a c# library and use in c++ projece or other language.

  • @baryalaybaz6224
    @baryalaybaz6224 Рік тому

    What if we want to use it in a seperate class not a controller. Can you explain that as well?

  • @stewtech
    @stewtech Рік тому +1

    Need a video explaining Asp.Net Core MVC Razor Pages!

  • @MsReader-ck7ni
    @MsReader-ck7ni 3 місяці тому

    Is creating an interface necessary? Can't i inject the class directly?

  • @736939
    @736939 2 роки тому +1

    What, if we have several services? Should I add them to the constructor? And what if my service also has a constructor where I should add the parameters?

    • @attilaguba856
      @attilaguba856 2 роки тому +1

      You can pass more services just need create a field and add them to constructor !

  • @philippebaillargeon5204
    @philippebaillargeon5204 Рік тому

    Ok, but what I want to know is how you register nested services in the Program.cs

  • @JustCode512
    @JustCode512 7 місяців тому

    please have a tutorial for rabbitMQ

  • @Koshala123
    @Koshala123 8 днів тому

    What is the difference between tutorialsEU - c# and TutorialsEU channel . Are those two done by the same company

  • @adeniyiajani7617
    @adeniyiajani7617 9 місяців тому

    The best way to upload an Excel file into database using Razor pages

  • @davestorm6718
    @davestorm6718 2 роки тому

    Ran into an issue with DI and using it outside of the pipeline (say, outside of a controller). No one seems to know how to use DI this way nor how to use it with a class library. Been told it's not possible, but I just can't believe this to be true.

    • @scmsean
      @scmsean 2 роки тому

      Using it with a class library is the whole point of it.

    • @davestorm6718
      @davestorm6718 2 роки тому

      @@scmsean - That's what I figured. I'm trying to use DI outside of a web application is all, but looks like I need to do a lot more work or reference something else (DotNet6). It should be simple, but tutorials either gloss over the important stuff, only use MVC, or are out of date.

    • @scmsean
      @scmsean 2 роки тому +1

      @@davestorm6718 Tutorials usually just show you simple stuff. They never help you with real world stuff. Since they are showing you simple, they are never are setup correctly.

    • @davestorm6718
      @davestorm6718 2 роки тому

      @@scmsean Agree! There's a lot of them that simply do things the wrong way, as well. Trying to find the "correct" way (in my case the Microsoft way), has been difficult - even the books online reads more like an index than a step by step guide, leaving more questions than answers. MS also keeps changing the game - they ditched the startup.cs, for example, and black-boxed a lot of things (EF is one example - if EF was more efficient, I'd still be using it - but it's still slow no matter how many optimizations you apply, terrible for microservices, insecure via sp_ExecuteSql , full of bloat and makes excessive calls to a database. Sure, it's easy, but the price is too high! - you spend days tweaking it then realize you could have just written DB code in ADO.Net - or used an ORM generator - and been done with it - faster!)
      I did a bulk insert with 10 million records: in raw ADO.Net code - it took 14 seconds to run, in highly optimized EF Core, it took a full minute!)

  • @natlinux
    @natlinux 2 роки тому

    Ich könnte ein einfaches Beispiel gebrauchen, über Account Registrierung und Logging mit SQL entity framework und dependency injection

  • @stuffy.design
    @stuffy.design 2 роки тому +4

    me, a 100% beginner watching this out of context with no idea on how to program in C# or what anything here means: ._.

  • @stevostevens8673
    @stevostevens8673 3 місяці тому

    SignalR for aspx

  • @user-jf5rd9oy4n
    @user-jf5rd9oy4n 10 місяців тому +1

    My english is very bad, but i have understood every your word. Great pronunciation. Thanx, you're awesome!

  • @nerdemcodigo
    @nerdemcodigo Рік тому

    Hi! Great video, you helped me lot here with a problem in injection on Startup. I apprecite if u can help me out with a problema that I'm having rn when I run my Application.
    "Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)"
    That erro appear to me in swagger, and I'm search for a long period of time and I did not found a solution, can you help?
    Thanks!