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

58797
141
2675
26.05.2022
Nick Chapsas

Nick Chapsas

14173719
145000
293
29.11.2011
GB
Описание видео:

Check out my new "Integration testing in ASP .NET Core" course and use code INTESTING1 for 20% off (first 300): 🤍nickchapsas.com/p/from-zero-to-hero-integration-testing-in-asp-net-core Become a Patreon and get source code access: 🤍🤍patreon.com/nickchapsas 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: Follow me on GitHub: 🤍bit.ly/ChapsasGitHub Follow me on Twitter: 🤍bit.ly/ChapsasTwitter Connect on LinkedIn: 🤍bit.ly/ChapsasLinkedIn Keep coding merch: 🤍keepcoding.shop #csharp #dotnet

Кадры из видео
The setup
The setup
The setup
The setup
Тэги из видео
Комментарии пользователей:
secrest
2022-09-05 14:09:36

Hey there nice video, but I think it could need a bit more detail on what actually happens and why the Options approach is so powerful .
The use-case I am talking about is what happens when multiple sources call the AddAwesomeness method.
So what really happens here behind the scenes is that the DI adds a lot of services to create and manage the state of an IOptions object.

The extension itself actually never creates the object itself or calls the delegate passed to it, instead there are multiple services involved in this process.
Your delegate will be registered as an I(Post)ConfigureOptions<T> instance to the DI.

The actual DEFAULT implementation of this process is like this:

1. Resolve an IEnumerable<IConfigureOptions<T> configureOptions
2. Resolve an IEnumerable<IPostConfigureOptions> postConfigureOptions
3. Resolve an IOptionsFactory<T> (using configureOptions & postConfigureOptions) factory
4. Resolve an IOptions<T> (using factory)

When calling IOptions<T>.Value it will
1. Create the OptionsObject
2. Invoke all implementations of IConfigureOptions<T> using the created instance.
3. Invoke all implementations of IPostConfigureOptions<T> using the created instance.
cache and return the final result.

If someone would do what you showed here and registeres the Options object in the DI and then calls this extension multiple times it would result in multiple instances of the object instead of a single one where all changes are aggregated.
I thought I would like to add this part because the other way would be very confusing to debug.

A Kerbers
2022-08-26 14:06:20

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?

watherby29
2022-08-11 08:05:55

Now no person above 50 can apply for an IT job because of this filter

Jeff Lindholm
2022-06-27 19:30:29

So I must be doing something wrong, since when I use the code where it is creating a new instance, the changes are never sent through to the controller's constructor.

Rayan
2022-06-24 21:07:16

i dont understand the use case of this trick

bladbimer
2022-06-20 00:53:45

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.

Pete Davis
2022-06-12 03:05:47

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.

sto ino
2022-06-10 11:33:09

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?

Crack
2022-06-09 15:19:22

random one... types in 69 :D :D

GardnerOne
2022-06-08 15:59:05

He definitely smirked at kIkbGXLkc-g&t=5m48s 5:48 😏

Seth Peasley
2022-06-08 14:29:45

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!

Edward de Leau
2022-06-06 23:49:34

Im currently using this to have each dll grab its correct configuration from a like-minded config tree entry + inject the connectionstring name in each of these so these can then be used in services (which use non orm transactions where connectionstring is needed) without needing to pass a global Configuration to these services where settingspath is the name of the executing assembly where . is replaced with : since ... passing values to this would mean that the caller would need to know the location of the setting but i want the component itself to know the location of the setting not the caller . The IConfiguration configuration then reads any configuration source e.g. jsons it can find.

public static readonly string AssemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? "X.Y.Z";
private static readonly string SettingsPath = AssemblyName.Replace('.', ':');

public static IServiceCollection AddXXX(this IServiceCollection services, IConfiguration configuration)
{
MyAwesomeoption _options = new MyAwesomeoption();
IConfigurationSection configSection = configuration.GetSection(SettingsPath);
configSection.Bind(_options);
string connectionString = configuration.GetConnectionString(_options.ConnectionName);
configuration[SettingsPath + ":ConnectionString"] = connectionString;
services
.Configure<MyAwesomeoption>(configuration.GetConnectionString(configuration.GetSection(SettingsPath))
.AddDbContextPool<MyContext>(options => { options.UseSqlServer(configuration.GetConnectionString(_options.ConnectionName});
return services;
}

Marc Yanus
2022-06-02 14:06:16

69. nice.

Klex
2022-06-02 00:40:35

Delegates are working, even quite efficiently, why change ? Nice syntax on it it sure, it'll take a bit to learn but sure.
They're hard to implement in the first place (properly) This just makes it more obscure.

Ranger1230
2022-06-01 12:42:14

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.

Abdul Rafey Artist
2022-06-01 09:35:55

Nice Age filter

Elsahr
2022-05-31 04:46:17

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 PostConfigure/IPostConfigureOptions registered. This can lead to issues where some validation will not fail or some values wont get set.

grimmersnee
2022-05-30 11:56:20

nice

Lee-Roy Ashworth
2022-05-30 09:25:38

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

Что ищут прямо сейчас
жаңылыктар 黒木ひかり apple rumors Keep You Safe รัชนก ศรีโลพันธุ์ クズの本懐 laudi landan se mysql for beginners सपनाको संकेत chinese new year remix дуба Shivling любо 五島列島 ポールワーク 日高 علیرضا پویا محمدرمضان benchmade rv 3 delay
Похожие видео
30.03.2022
Writing C# without allocating ANY memory

Writing C# without allocating ANY memory

17.04.2022
Storing secrets CORRECTLY in .NET using AWS Secrets Manager

Storing secrets CORRECTLY in .NET using AWS Secrets Manager

09.07.2021
What is Span in C# and why you should be using it

What is Span in C# and why you should be using it

16.03.2021
The coding conventions I use for C# and why I use them

The coding conventions I use for C# and why I use them

05.01.2022
You are doing .NET logging wrong. Let's fix it

You are doing .NET logging wrong. Let's fix it

19.06.2021
Is Parallel.For/ForEach in C# actually worth it? (and how to modernize it)

Is Parallel.For/ForEach in C# actually worth it? (and how to modernize it)

31.08.2022
How IEnumerable can kill your performance in C#

How IEnumerable can kill your performance in C#

15.05.2022
The .NET dependency injection methods you are not using

The .NET dependency injection methods you are not using

28.05.2022
Why I won’t need constructors anymore in C# 11

Why I won’t need constructors anymore in C# 11

09.09.2021
5 open source .NET projects that deserve more attention

5 open source .NET projects that deserve more attention

11.04.2021
What are Closures in C# and why you NEED to know about them

What are Closures in C# and why you NEED to know about them

15.07.2022
The NEW caching you should be using in .NET 7

The NEW caching you should be using in .NET 7

07.09.2022
The CORRECT way to implement Retries in .NET

The CORRECT way to implement Retries in .NET

08.06.2022
Fixing the performance problem of enums in C#

Fixing the performance problem of enums in C#

10.06.2022
The weirdest C# 11 feature but also the best one

The weirdest C# 11 feature but also the best one

07.07.2022
SingleOrDefault or FirstOrDefault? When LINQ might harm you

SingleOrDefault or FirstOrDefault? When LINQ might harm you