|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: .NET Injection of a dependency list |
| 4 | +date: '2022-01-22 08:00:00' |
| 5 | +image: /images/posts/ivan-diaz-_ts3NfjvaXo-unsplash-min.jpg |
| 6 | +tags: |
| 7 | +- c-sharp |
| 8 | +- dotnet |
| 9 | +- developer |
| 10 | +- dependency-injection |
| 11 | +- clean-code |
| 12 | +--- |
| 13 | + |
| 14 | +Whilst doom scrolling Twitter last night, I came across a tweet by a Norweigan software developer named <a href="https://twitter.com/KarlSolgard" target="_blank" title="Karlsolgrad on Twitter">KarlSolgard</a>; he tweeted about a feature of .NET Core Dependency Injection that I was unaware existed. |
| 15 | + |
| 16 | +<blockquote class="twitter-tweet"><p lang="en" dir="ltr"><a href="https://twitter.com/hashtag/dotnet?src=hash&ref_src=twsrc%5Etfw">#dotnet</a> tip:<br>You can register services with same interface and inject them as a list in ctor <a href="https://t.co/pa8hhSGIsg">pic.twitter.com/pa8hhSGIsg</a></p>— Karl 🔥 (@KarlSolgard) <a href="https://twitter.com/KarlSolgard/status/1484527534742114306?ref_src=twsrc%5Etfw">January 21, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> |
| 17 | + |
| 18 | +Injecting a class or a factory is something that every .NET developer has come to know; however, you can inject an IEnumerable of something. By registering multiple implementations of an interface, your classes can ask for an array of concrete implementations of that interface. |
| 19 | +<!--more--> |
| 20 | + |
| 21 | +<!--kg-card-begin: markdown--> |
| 22 | + |
| 23 | + var services = new ServiceCollection() |
| 24 | + .AddSingleton<IMessageHandler, EmailHandler>() |
| 25 | + .AddSingleton<IMessageHandler, SmsHandler>() |
| 26 | + .AddSingleton<IDataSource, FakeDataSource>() |
| 27 | + .AddSingleton<IMessageSender, MessageSender>() |
| 28 | + .BuildServiceProvider(); |
| 29 | + |
| 30 | + var dataSource = services.GetService<IDataSource>(); |
| 31 | + var messages = await dataSource!.GetPendingMessagesAsync(); |
| 32 | + var sender = services.GetService<IMessageSender>(); |
| 33 | + |
| 34 | + await sender!.SendMessagesAsync(messages); |
| 35 | + |
| 36 | +<!--kg-card-end: markdown--> |
| 37 | + |
| 38 | +I asked the tweet's author for an <a href="https://t.co/pknmCiX9nG" target="_blank" title="FizzBuzz4Champs">example</a>, and they replied with a way to tackle the traditional <a href="https://en.wikipedia.org/wiki/Fizz_buzz" target="_blank">FizzBuzz challenge</a>, but I wasn't satisfied and challenged myself to look for a more practical example. |
| 39 | + |
| 40 | +Sending messages to different services based on some internal logic is often a pattern I have to write. So I went away and wrote a basic message sending system that doesn't contain any hardcoded logic to determine how to send different messages. |
| 41 | + |
| 42 | +The code in <a href="https://github.com/melodiouscode/demo-Multi_IOC_DI" target="_blank" title="My Demo Repository">my demo repository</a> isn't a complete example, and it may be a bit contrived, but it does show a practical implementation of the 'IEnumerable Injection' pattern. |
| 43 | + |
| 44 | +<small>Cover Photo by <a href="https://unsplash.com/@ivvndiaz?utm_source=melodiouscode&utm_medium=referral&utm_content=creditCopyText">@ivvndiaz</a> on <a href="https://unsplash.com/photos/_ts3NfjvaXo?utm_source=melodiouscode&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></small> |
0 commit comments