MV10 / mv10.github.io

McGuireV10's personal blog
MIT License
4 stars 2 forks source link

.NET Core Class Library Dependency Injection - Forty Years of Code #48

Open utterances-bot opened 4 years ago

utterances-bot commented 4 years ago

.NET Core Class Library Dependency Injection - Forty Years of Code

Adding dependency injection to class libraries in .NET Core 2.0 is almost as easy as DI in ASP.NET, but class libraries may require a few extra tricks that aren’t immediately obvious.

https://mcguirev10.com/2018/01/31/net-core-class-library-dependency-injection.html

dcuccia commented 4 years ago

Nice article, thanks! Helped me think critically about where the Service Locator pattern still makes sense. I do have to say - while I understand the first example, it still smells to me like a different architecture could be less confusing to work with/grok. SchemaCachedDatabase : Database, for example. I'm not a deep inheritance fan, but wouldn't this make things less "wobbly" and injectable/mockable?

jeffjohnson commented 4 years ago

How would you do this with overloaded constructors?

MV10 commented 4 years ago

@jeffjohnson I haven't done it myself, but as I understand recent versions of DI will auto-select the constructor with the largest number of arguments it can populate. Other constructors are simply ignored.

rolandzpl commented 4 years ago

I am trying to implement similar extensions for IServiceCollection and I found an issue: if I refer to concrete Micosoft.Extensions.DependencyInjection and then use my assembly/nuget in a project (core 3.1, net 5, or whatether else in the future) I may easily face conflicts between versions of that Microsoft.Extensions... being used.

Did you manage to solve such an issue anyhow?

Thanks in advance,

Roland

MV10 commented 4 years ago

@rolandzpl I think the solution would be a NuGet package that targets multiple frameworks via conditional TargetFramework entries in the csproj. This is how we did it in Serilog for both .NET Core and .NET Framework:

https://github.com/serilog/serilog-settings-configuration/blob/dev/src/Serilog.Settings.Configuration/Serilog.Settings.Configuration.csproj

AamilShohail commented 3 years ago

Thanks in advance for Knowledge transfer

Greg90277 commented 2 years ago

Nice job! I think though it wanders into the theoretical without providing a simple example of how to actually use the example started at the beginning. Can you just show at its most simplistic how to actually consume the DI service in the class library? I wasn't able to figure it out from the service locator example provided. Thanks!

MV10 commented 2 years ago

@Greg90277 thanks for the comment. My expectation was that the reader already understands constructor-driven DI, and the intent was to show that you don't need ASP.NET to use it (since that's where most .NET devs encounter it first). But I don't disagree, it would probably make sense to add one more paragraph and code block to show one of the example services actually being invoked. I'll put it on my to-do list.

Greg90277 commented 2 years ago

Hi Jon.

Wow – didn’t expect a response – especially so quickly!

Thank you for your posts and the hard work you put into them – they really are valuable, and you do a great job.

Cheers and thanks again!!

Greg

Greg Dobbs, President dobbs enterprise solutions, inc. http://www.dobbs1.comhttp://www.dobbs1.com/ Managing Your Success!

@.***

From: Jon McGuire @.> Sent: Saturday, March 19, 2022 8:39 AM To: MV10/mv10.github.io @.> Cc: Greg Dobbs @.>; Mention @.> Subject: Re: [MV10/mv10.github.io] .NET Core Class Library Dependency Injection - Forty Years of Code (#48)

@Greg90277https://github.com/Greg90277 thanks for the comment. My expectation was that the reader already understands constructor-driven DI, and the intent was to show that you don't need ASP.NET to use it (since that's where most .NET devs encounter it first). But I don't disagree, it would probably make sense to add one more paragraph and code block to show one of the example services actually being invoked. I'll put it on my to-do list.

— Reply to this email directly, view it on GitHubhttps://github.com/MV10/mv10.github.io/issues/48#issuecomment-1073031172, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AE5R7VSPPOTTF57TL3HGCJDVAXYI3ANCNFSM4LVW742A. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you were mentioned.Message ID: @.**@.>>