The setup 'trick' that .NET libraries use and you should too

preview_player
Показать описание

Hello everybody I'm Nick and in this video I will show you how you can use a feature of C# that isn't obvious at first glance. This feature is used extensively by ASPNET Core and every popular .NET library out there and it focuses on simplifying how you can provide configuraiton details and settings.

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet
Рекомендации по теме
Комментарии
Автор

Finally!!!! He covered something I already knew lol 😆. But still a great video as always!

travisehillsjr
Автор

Great video! I'm trying to level up my software engineering skills, and your videos on Dependency Injection are great! The MS documentation often has a real "A monad is just a monoid in the category of endofunctors" energy, which frustrates me to no end. Your videos really help fill in the gaps. I see some folks complaining this video was too simple but it was perfect for me! Thank you!

sarah_
Автор

I was just about to stop the video, and go look at the framework source code to see how it was implemented, and then you said, "...but I want you to know what happens behind the scenes..."
Thank you, sir!
Love your videos man. I'm a big believer in pair programming. If for no other reason, than because you pick up little bits and pieces... tricks and tips... from every developer you work with. And the more you work with them, the more of their tricks you learn. Those things can really improve a developer's productivity more than just about anything else, I think. At least when you're kind of at the more advanced level. Watching your videos is like pair programming with a really talented developer who's way more up on the latest stuff than I am. Beyond the intended content, which is always excellent and I learn a bunch from every time, I just get so much from how you do things. IDE stuff, shortcuts I didn't know existed, extensions that do really cool stuff, etc.
You and Jon Skeet should get together and do a C# "Master of Masters" Class. lol.

petedavis
Автор

I use that approach in combination with IConfiguration binding . Very powerful stuff.

astralpowers
Автор

Thanks for this, it's something I've been wondering about but never really took the time to investigate.

simonm
Автор

I use the Action/Func delegates effectively when dealing with complex and/or disposable objects.
I set up the disposable object with default values or parameter-fed ones. Then the last param would be the Action<> or Func<> which contains the substantiated object allowing for a much simpler mutation method.

It's really designed for other devs on a project to use a thing without fully understanding it. It allows for polymorphism, passing in any version of type; such as a semi-configured type that doesn't need reconfiguring.

Overengineering at its finest.

asteinerd
Автор

One part of this options extension convention that is missing is to return IServicesCollection from your extension method, which allows other extensions to continue to be chained. It's always annoying when libraries have a void return for IServicesCollection extensions. Why do they want to break my flow!? 😀

seangwright
Автор

Invoking is a great way to create an initialised Options object. However this object can be different to the one received from DI. Invoking the action will not call any registered. This can lead to issues where some validation will not fail or some values wont get set.

elsahr_
Автор

i love this "pattern" and remember learning how to use it with my own libraries after learning how to use Entity Framework. great video!

Zashxq
Автор

Hi Nick. Just wanted to say that I really like your courses so far (well, I am still busy with the Minimal API course but it is great so far). I was afraid that it would go too fast as your videos tend to assume a certain level of knowledge and you go over certain parts really fast or skip them all together (which is just me and my lack of certain core concepts that trips me up) but your courses are totally different. You really explain things from the basics to the advanced level and go through them slowly and in a structured manner. Your knowledge of the subject really shows. Thank you so much for making me a better developer

dennisvandermeer
Автор

I use this to setup data objects for my tests, works like a charm!

raphaelmt
Автор

Thank you! I had no idea you could just hand the action to the .Configure method to inject the IOptions. The whole time I had been manually registering a factory that uses the action, but this is much cleaner.

Ranger
Автор

Ah, so it’s a post-constructor modify the contents of the object. The method that receives this action initialises everything, but this parameter lets you tweak anything about that object - but they might create YYY and only let you access an interface or base class with designated customization fields.

abj
Автор

Great I love it this approach too. But we have to take care about default values or do some validation when something is missing.

michastachyra
Автор

Pretty gnarly what that Action.Invoke(arg) method is doing to the arg. It wouldn't be obvious normally that it's injecting it own internal version of "arg" into the original arg you used to invoke it! You'd think that invoking a delegate with some argument, would actually use THAT argument, not replace it with something else before using it.

PaulSebastianM
Автор

Very cool, I started creating a library that needs to do something like this. Very nice explanation 👍

LeeRoyAshworth
Автор

I am probably missing something but I really don't get the point here.
It's obviously A way of doing it and its nice to know how it works under the hood but I think just seeing the lambda parameter is fairly self-explanatory in how this might be implemented under the hood.
My main question, which this video seems to have completely missed is what exactly are the advantages of doing this?
The only thing I can come up with of why this is a useful pattern is if your configuration class has some complex setters or properties that can not be set through an object initializer especially in older versions of C#.
Is this about using one unified approach everywhere for configuration passing even if its just unnecessary shenanigans in many straight-forward cases?

lexer_
Автор

Nice video, got to know about cool stuff. So, would it even bind options from configuration object and values in appsettings.json file?

microtech
Автор

Hi Nick, honnestly i really like the content of your videos as c# is really the language i am working as a day to day. For this video i understand that it work to use an action instead of creating an object but what i dont really know is why not using an object. Usualy i like adding complexity or more layer when it provides feature or prevent issues but here i dont get it. I can inderstand the lazy loading strategy but that's all. Thanks for your help.

bladbimer
Автор

Simple and easy explanation and demo as always, great work!
I like to use this approach as well, but usually I struggle to use this together with IConfiguration. I have the feeling I miss something to make it elegant. Can you follow up with a more extended example and the usage of IConfigration?

stoino