Dependency injection and clean service registration | ASP.NET 5 REST API Tutorial 3

  Рет қаралды 55,800

Nick Chapsas

Nick Chapsas

Күн бұрын

Become a Patreon and get source code access: / nickchapsas
Check out my courses: dometrain.com
Hello everybody I'm Nick and today I will show you how you can clean up your Startup file. We will make our service registration closed for modification and open for extension.
Don't forget to comment, like and subscribe :)
Social media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
#tutorial #rest #api

Пікірлер: 87
@nickchapsas
@nickchapsas 5 жыл бұрын
My thoughts have changed in this approach ever since, because it doesn't involve things like the order that services might need to be registered. I will be revisiting this in the future.
@Gxelha
@Gxelha 5 жыл бұрын
Correct, but this approach is very neat when the order is not needed.
@svenkicherer
@svenkicherer 4 жыл бұрын
looking forward to this as it could be pretty useful as some services need other services registered before themselves.
@ophir1982
@ophir1982 4 жыл бұрын
Maybe a better approach is to use extension methods that encapsulate common service registration and logic and calling them in the order you want. It's not as cool as having those classes automatically register, but it is clean and by the order the developer chose to put it.
@ophir1982
@ophir1982 4 жыл бұрын
@Florin Bobiș Thought about this option, but then you need to start managing this index. And this could be a source for trouble. K.I.S.S
@anthoniG
@anthoniG 4 жыл бұрын
@@ophir1982 This is what I use. I have multiple methods on one extension class, each named for they add (for example AddIdentityCore, AddDatabase, etc) I then just them in the ordered required
@mac19999x
@mac19999x 3 жыл бұрын
God... friday ive been working on adding an Installer to our framework. Sunday, in order to improve a bit im watching your vids, and you're actually doing what im planning to do. The coincidence is just amazing.
@soucianceeqdamrashti8175
@soucianceeqdamrashti8175 3 жыл бұрын
Really like the clean startup and put everything in their own installer. Easy to add and remove without changing startup. Nice!
@ViragDesai
@ViragDesai 3 жыл бұрын
This is simply amazing! So much to learn from you! Fabulous! Thank you, Nick! Your fan now.
@jaykbg7669
@jaykbg7669 4 жыл бұрын
Very impressive coding skills. I'm enjoying the best practices
@faridmammadov4277
@faridmammadov4277 4 жыл бұрын
Thanks for this tutorials about web services i cant find normal tutorual about this amazing thing. but i understand your videos its very easy and understandable.
@guroo1000
@guroo1000 4 жыл бұрын
Simply brilliant!
@sunnyokoro
@sunnyokoro 4 жыл бұрын
1000 likes for this. I love your coding pattern, very clean.
@PatricSjoeoe
@PatricSjoeoe 3 жыл бұрын
I like this clean solution a lot! thanks :)
@sundusalharbi5713
@sundusalharbi5713 2 жыл бұрын
Great video, many thanks
@max_san2221
@max_san2221 4 жыл бұрын
Thanks for this tutorials.
@ashishdeora8522
@ashishdeora8522 4 жыл бұрын
Thank you for this great course, Nick. We love you. However, apparently, there was no need to make it so concise, calling of the services. It's ok to have two lines instead of one. Below type of code looks perfectly fine and works great. What you did would help us to manage an assembly of services while there is actually no need of it now as you are introducing the basics; these optimization techniques should be kept out of scope in my view, just to avoid any feelings of complexes public void ConfigureServices(IServiceCollection services) { new DbInstaller().InstallServices(services, Configuration); new MvcInstaller().InstallServices(services, Configuration); } Thank you once again for your contribution to the community of developers. You are an inspiration
@cicher
@cicher 3 жыл бұрын
Also the installers could be statics classes and the install methods extensions of IServicesCollection, then you don't have to instantiate them. This is the approach used with all official SDKs.
@PelFox
@PelFox 4 жыл бұрын
Neat method. I tend to extend each DI containers to each project in a class named DependencyInjections. So my Data Layer will extend services to add the dependencies needed for that layer. Then in my startup I will run eg) services.AddData(); and services.AddBusiness();
@denizarca5471
@denizarca5471 4 жыл бұрын
Hi Nick, was this approach revisited to install things in a certain order? Great series on REST with .NET Core by the way!
@dormelamed5974
@dormelamed5974 3 жыл бұрын
Great videos my friend, helps a lot! one note, you need to increase the font, it is very hard to understand when it is so small. keep up with your good work man!!
@henturuk
@henturuk 4 жыл бұрын
thanks very esasy and clear
@davidpittman9664
@davidpittman9664 4 жыл бұрын
Great approach for cleaning up the startup code. I'm not sure that the DbInstaller would work with a multiple database server environments as the current 'DefaultConnection' only applies to one server/database.
@akbare-z815
@akbare-z815 4 жыл бұрын
Very nice sir.
@shashankpandey1019
@shashankpandey1019 4 жыл бұрын
Great man 😍😍 Where were you till now 😬
@davidmiko1102
@davidmiko1102 3 жыл бұрын
Really nice and clean service registration, really loved it. Also I have a question: Do you think it's a good practice to write doc comments to all methods that I write or just code should be self-descriptive? Really sorry for my english.
@yuyuewang6405
@yuyuewang6405 4 жыл бұрын
OMG, thanks for teaching us this amazing approach, I wander how many approaches like this in numerous projects in the world, it is absolute not friendly to beginners, people will easily get confused, but this is the necessary path to a master. Regarding to services registered order, I think at least we can keep the extension method, and remove foreach, or we can abstract another layer for orderless service than only loop it.
@ubakangene2067
@ubakangene2067 4 жыл бұрын
please how can i run the services registered without the foreach()
@yuyuewang6405
@yuyuewang6405 4 жыл бұрын
@@ubakangene2067 I also didn't quite get the foreach pare, what I was doing is just use extension method, and register all service one by one like before
@paladincubano
@paladincubano 4 жыл бұрын
Thanks... you are a f.... master
@MITHUNROYabir
@MITHUNROYabir 4 жыл бұрын
Thanks for your great in depth rest tutorial. Please make code font a little bigger. It would easy to follow on youtube.
@Vivalavidapool
@Vivalavidapool 5 жыл бұрын
cool approach! I'm curious how'd you unit test your DI registrations and startup class. More importantly, where did you find the documentation and examples how to use startup class and ExportableTypes, Activator.CreateInstance methods. I wanna learn all the features of startup and ways to manipulate it, but I'm struggling to find anything on the web. Thanks Nick!
@rahmatabadi3839
@rahmatabadi3839 4 жыл бұрын
He used reflection (dynamic invocation) and it becomes slower. But his idea is great!
@ubakangene2067
@ubakangene2067 4 жыл бұрын
Nick is awesome and being new to the .Net ecosystem i stumbled on this early enough on my journey,only challenge right now is i am using the Visual Studio 2019 Community and my compiler has a problem with the ForEach(installer => installer.installServices(services, Configuration)), i need solutions to this effect, cause i enjoy practicing this implementation and it has become quite personal.
@roddydairion7252
@roddydairion7252 4 жыл бұрын
@@ubakangene2067 Late reply to your comment, hopefully it will help others. Somehow in my version of .netcore 2.1, the Method parameters has switched ForEach(installer => installer.installServices(Configuration, services))
@lekabros
@lekabros 3 жыл бұрын
@@rahmatabadi3839 it’s not his idea, it’s Jason Taylor’s Clean Architecture idea..
@NuKleArNyPL
@NuKleArNyPL Жыл бұрын
Thanks, my Program.cs is clean now
@kayandersen2828
@kayandersen2828 3 жыл бұрын
Very cool
@pooyalaryan4455
@pooyalaryan4455 Жыл бұрын
Just WOOOOOOOOOOOOOOOOOOOOOOOOW
@joergw
@joergw 4 жыл бұрын
We are using a very similar approach
@serhiihorun6298
@serhiihorun6298 4 жыл бұрын
Thanks
@thanhtai17
@thanhtai17 4 жыл бұрын
Hi Nick, How can I Implement Installer in another project without Startup file???
@Sebastian----
@Sebastian---- 2 жыл бұрын
Isn't this basically the same as modules in ninject or structure map. Normally the code that use the IServiceCollection is minimal, compared to the rest of the DJ. Did i is missed something?
@jawadahmed7336
@jawadahmed7336 Жыл бұрын
will this work on three layered architecture?
@prasannahiremath442
@prasannahiremath442 Жыл бұрын
How do you register services in latest version of .Net where we don't have Startup.cs ?
@hatimazizi218
@hatimazizi218 4 жыл бұрын
hello thank you very much for this course , and i have two question not very close to the tutorial please : does the enterprise use the MVC application or API application ? i know the fundamentals of asp.net core but i d'ont want to waste time learnin stuffs like (razor pages , viewComponent , tagHelper, htmlHelpers ...) can i jump all of this stuff and learn asp.net core api ? :)
@DeepakShaw
@DeepakShaw Жыл бұрын
Hey Nick, Can you make a video on Program.cs cleanup (.net 7). Thanks.
@nayanchoudhary4353
@nayanchoudhary4353 2 жыл бұрын
Clean, and would work for independent services without any specific conditions! For others, this way can be avoided. Like, registration in a particular order, or registering a single kind of installer.
@aaronaaronaaron5922
@aaronaaronaaron5922 4 жыл бұрын
Hi Nick, thank you very much for this grats videos!! Just one comment, I am having troubles with the resolution of this video :(
@nickchapsas
@nickchapsas 4 жыл бұрын
I am so sorry about this. This gets better in future videos. Try to switch it to 1080p.
@coffee.coyote
@coffee.coyote 4 жыл бұрын
how can you use installers form diffent projects in solution? is there any clean generic way to get IInstallers from all projects in solution?
@coffee.coyote
@coffee.coyote 4 жыл бұрын
I made it this way: public static IEnumerable GetAssembliesIdExecutingDirectory(Func predicate = null) { if (predicate == null) predicate = asmName => true; string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); IEnumerable assembliesPaths = Directory .GetFiles(assemblyFolder, "*.dll") .Where(asmPath => predicate(Path.GetFileName(asmPath))); foreach (string asmPath in assembliesPaths) yield return Assembly.LoadFrom(asmPath); }
@dayv2005
@dayv2005 5 жыл бұрын
Might be a dumb question but when you hover over your variables to auto import their namespace, I don't get the option to auto import. Any idea of what I have set up wrong for this not to work?
@nickchapsas
@nickchapsas 5 жыл бұрын
On this video I am using Resharper. It's a feature of this extension. You can read about resharper here: www.jetbrains.com/resharper/
@RyanTuller
@RyanTuller 5 жыл бұрын
Look up the hotkey for this. I believe it's "space + enter"
@dayv2005
@dayv2005 5 жыл бұрын
@@nickchapsas Thanks. Will look into this. Also thanks for the quick reply. Going through your playlist for this and loving it so far.
@nickchapsas
@nickchapsas 5 жыл бұрын
@@RyanTuller It's Alt+Enter with Resharper
@stevenbc9597
@stevenbc9597 3 жыл бұрын
What happens when you need to add installers in a specific order?
@nickchapsas
@nickchapsas 3 жыл бұрын
Then you bake an order integer for your installers and sort before wiring up
@abdullahalmousa9181
@abdullahalmousa9181 3 жыл бұрын
I don't understand why you keep the UseMVC() what is the benefit of it ?
@nickchapsas
@nickchapsas 3 жыл бұрын
It should be UseMvcCore tbh. It provides some routing
@diji1102
@diji1102 3 жыл бұрын
why did the clock go from 20 58 to 21
@bishoynabil5303
@bishoynabil5303 4 жыл бұрын
thank you but The font size is too bad
@anggiangnam2389
@anggiangnam2389 5 жыл бұрын
Hy gui, can you upload this video again, this quality is to bad
@jerryjeremy4038
@jerryjeremy4038 4 жыл бұрын
Aren't you reinventing the wheel with this approach?
@adamschneider868
@adamschneider868 2 жыл бұрын
This is neat, but after I typed it all in I decided to roll back my change and just stick with how it is.
@josbexerr5166
@josbexerr5166 4 жыл бұрын
Que tal dominio del tema....
@AlaaOmar
@AlaaOmar 3 жыл бұрын
Hello The font is very small
@TheCameltotem
@TheCameltotem 3 жыл бұрын
I get that it's more seperated and all but this seems too much work for a whole lot of nothing. I mean you're not touching the startup class too often.
@nickchapsas
@nickchapsas 3 жыл бұрын
Are you not? In my experience you are touching it in some form every single time you are adding some piece of functionality in your project. The alternative would be extension methods and when a new group is created you need to go back to it and edit it.
@IAmTechDevsAspNet
@IAmTechDevsAspNet 4 жыл бұрын
var installers count 0(zero) in .net core 3.1 how is resolve ?
@gergocsizmadia7329
@gergocsizmadia7329 4 жыл бұрын
Look at the github source code it updated and it will be resolve your problem. :) That worked for me.
@velkumars26
@velkumars26 2 жыл бұрын
@@gergocsizmadia7329 where is the source ?
@jhonhernandez9210
@jhonhernandez9210 3 жыл бұрын
Its really hard to see if you don´t zoom, man
@expatca4444
@expatca4444 2 жыл бұрын
Sorry but this is not needed at all. KISSSSSSSS !!!
Creating resources using POST | ASP.NET 5 REST API Tutorial 4
11:44
The .NET dependency injection methods you are not using
11:49
Nick Chapsas
Рет қаралды 91 М.
New Gadgets! Bycycle 4.0 🚲 #shorts
00:14
BongBee Family
Рет қаралды 13 МЛН
ПООСТЕРЕГИСЬ🙊🙊🙊
00:39
Chapitosiki
Рет қаралды 37 МЛН
Super gymnastics 😍🫣
00:15
Lexa_Merin
Рет қаралды 18 МЛН
Dependency Injection | Blazor Tutorial 2
8:18
Nick Chapsas
Рет қаралды 17 М.
Why Developers Hate "Clean Code"?
14:39
Nick Chapsas
Рет қаралды 42 М.
Integration testing | ASP.NET Core 5 REST API Tutorial 15
22:13
Nick Chapsas
Рет қаралды 110 М.
Structuring Dependency Injection In ASP.NET Core The Right Way
16:22
Milan Jovanović
Рет қаралды 48 М.
Dependency Injection for ASP NET Core 6.0 - An UPDATED 2022 Tutorial
8:10
"Stop Using Async Await in .NET to Save Threads" | Code Cop #018
14:05
New Gadgets! Bycycle 4.0 🚲 #shorts
00:14
BongBee Family
Рет қаралды 13 МЛН