%3CLINGO-SUB%20id%3D%22lingo-sub-1502394%22%20slang%3D%22en-US%22%3EDependency%20Injection%20on%20Azure%20Functions%3A%205%20Ways%20Selecting%20Instance%20from%20Instances%20of%20Same%20Interface%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1502394%22%20slang%3D%22en-US%22%3E%3CP%3EWhile%20building%20an%20%3CA%20title%3D%22Azure%20Functions%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-functions%2Ffunctions-overview%3FWT.mc_id%3Dappsonazure-techcommunity-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Functions%3C%2FA%3E%20application%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22Setting%20an%20IoC%20container%20for%20dependency%20injection%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-functions%2Ffunctions-dotnet-dependency-injection%3FWT.mc_id%3Dappsonazure-techcommunity-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esetting%20an%20IoC%20container%20for%20dependency%20injection%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bhas%20many%20benefits%20by%20comparing%20to%20just%20using%20the%20static%20classes%20and%20methods%3C%2FSPAN%3E.%20Azure%20Functions%20leverages%20the%20built-in%20IoC%20container%20featured%20by%20%3CA%20title%3D%22ASP.NET%20Core%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Faspnet%2Fcore%2F%3Fview%3Daspnetcore-3.1%26amp%3BWT.mc_id%3Dappsonazure-techcommunity-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EASP.NET%20Core%3C%2FA%3E%20that%20is%20easy%20to%20use%2C%20without%20having%20to%20rely%20on%20any%20third-party%20libraries.%20Throughout%20this%20post%2C%20I'm%20going%20to%20discuss%20five%20different%20ways%20to%20pick%20up%20a%20dependency%20injected%20from%20multiple%20instances%20sharing%20with%20the%20same%20interface.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22implementing-interface-and-classes%22%20id%3D%22toc-hId--1301018478%22%20id%3D%22toc-hId--1301018478%22%3EImplementing%20Interface%20and%20Classes%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20assume%20that%20we're%20writing%20one%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EIFeedReader%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Einterface%20and%20three%20different%20classes%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EBlogFeedReader%3C%2FCODE%3E%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EPodcastFeedReader%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EYouTubeFeedReader%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eimplementing%20the%20interface.%20There's%20nothing%20fancy.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20interface%20IFeedReader%0A%7B%0A%20%20%20%20string%20Name%20%7B%20get%3B%20%7D%0A%20%20%20%20string%20GetSingleFeedTitle()%3B%0A%7D%0A%0Apublic%20class%20BlogFeedReader%20%3A%20IFeedReader%0A%7B%0A%20%20%20%20public%20BlogFeedReader()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20this.Name%20%3D%20%22Blog%22%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20string%20Name%20%7B%20get%3B%20%7D%0A%0A%20%20%20%20public%20string%20GetSingleFeedTitle()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20%22This%20is%20blog%20item%201%22%3B%0A%20%20%20%20%7D%0A%7D%0A%0Apublic%20class%20PodcastFeedReader%20%3A%20IFeedReader%0A%7B%0A%20%20%20%20public%20PodcastFeedReader()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20this.Name%20%3D%20%22Podcast%22%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20string%20Name%20%7B%20get%3B%20%7D%0A%0A%20%20%20%20public%20string%20GetSingleFeedTitle()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20%22This%20is%20audio%20item%201%22%3B%0A%20%20%20%20%7D%0A%7D%0A%0Apublic%20class%20YouTubeFeedReader%20%3A%20IFeedReader%0A%7B%0A%20%20%20%20public%20YouTubeFeedReader()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20this.Name%20%3D%20%22YouTube%22%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20string%20Name%20%7B%20get%3B%20%7D%0A%0A%20%20%20%20public%20string%20GetSingleFeedTitle()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20%22This%20is%20video%20item%201%22%3B%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22ioc-container-registration-1--collection%22%20id%3D%22toc-hId-1186494355%22%20id%3D%22toc-hId-1186494355%22%3EIoC%20Container%20Registration%20%231%20%E2%80%93%20Collection%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20register%20all%20the%20classes%20declared%20above%20into%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EConfigure()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20in%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EStartup.cs%3C%2FCODE%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20override%20void%20Configure(IFunctionsHostBuilder%20builder)%0A%7B%0A%20%20%20%20builder.Services.AddTransient%3CIFEEDREADER%3E()%3B%0A%20%20%20%20builder.Services.AddTransient%3CIFEEDREADER%3E()%3B%0A%20%20%20%20builder.Services.AddTransient%3CIFEEDREADER%3E()%3B%0A%7D%3C%2FIFEEDREADER%3E%3C%2FIFEEDREADER%3E%3C%2FIFEEDREADER%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EBy%20doing%20so%2C%20all%20three%20classes%20have%20been%20registered%20as%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EIFeedReader%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Binstances.%20However%2C%20from%20the%20consumer%20perspective%2C%20it%20doesn't%20know%20which%20instance%20is%20appropriate%20to%20use.%20In%20this%20case%2C%20using%20a%20collection%20as%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EIEnumerable%3CIFEEDREADER%3E%3C%2FIFEEDREADER%3E%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bis%20useful.%20In%20other%20words%2C%20inject%20a%20dependency%20of%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EIEnumerable%3CIFEEDREADER%3E%3C%2FIFEEDREADER%3E%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bto%20the%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EFeedReaderHttpTrigger%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bclass%20and%20filter%20one%20instance%20from%20the%20collection.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20class%20FeedReaderHttpTrigger%0A%7B%0A%20%20%20%20private%20readonly%20IFeedReader%20_reader%3B%0A%0A%20%20%20%20public%20FeedReaderHttpTrigger(IEnumerable%3CIFEEDREADER%3E%20readers)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20this._reader%20%3D%20readers.SingleOrDefault(p%20%3D%26gt%3B%20p.Name%20%3D%3D%20%22Blog%22)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%5BFunctionName(nameof(FeedReaderHttpTrigger.GetFeedItemAsync))%5D%0A%20%20%20%20public%20async%20Task%3CIACTIONRESULT%3E%20GetFeedItemAsync(%0A%20%20%20%20%20%20%20%20%5BHttpTrigger(AuthorizationLevel.Function%2C%20%22post%22%2C%20Route%20%3D%20%22feeds%2Fitem%22)%5D%20HttpRequest%20req%2C%0A%20%20%20%20%20%20%20%20ILogger%20log)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20title%20%3D%20this._reader.GetSingleFeedTitle()%3B%0A%0A%20%20%20%20%20%20%20%20return%20new%20OkObjectResult(title)%3B%0A%20%20%20%20%7D%0A%7D%3C%2FIACTIONRESULT%3E%3C%2FIFEEDREADER%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIt's%20one%20way%20to%20use%20the%20collection%20as%20a%20dependency.%20The%20other%20way%20to%20use%20the%20collection%20is%20to%20use%20a%20loop.%20It's%20useful%20when%20we%20implement%20either%20a%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.dofactory.com%2Fnet%2Fvisitor-design-pattern%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EVisitor%20Pattern%3C%2FA%3E%26nbsp%3Bor%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.dofactory.com%2Fnet%2Fiterator-design-pattern%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EIterator%20Pattern%3C%2FA%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20class%20FeedReaderHttpTrigger%0A%7B%0A%20%20%20%20private%20readonly%20IEnumerable%3CIFEEDREADER%3E%20_readers%3B%0A%0A%20%20%20%20public%20FeedReaderHttpTrigger(IEnumerable%3CIFEEDREADER%3E%20readers)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20this._readers%20%3D%20readers%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%5BFunctionName(nameof(FeedReaderHttpTrigger.GetFeedItemAsync))%5D%0A%20%20%20%20public%20async%20Task%3CIACTIONRESULT%3E%20GetFeedItemAsync(%0A%20%20%20%20%20%20%20%20%5BHttpTrigger(AuthorizationLevel.Function%2C%20%22post%22%2C%20Route%20%3D%20%22feeds%2Fitem%22)%5D%20HttpRequest%20req%2C%0A%20%20%20%20%20%20%20%20ILogger%20log)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20title%20%3D%20default(string)%3B%0A%20%20%20%20%20%20%20%20foreach%20(reader%20in%20this._readers)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(reader.Name%20!%3D%20%22Blog%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20title%20%3D%20reader.GetSingleFeedTitle()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20return%20new%20OkObjectResult(title)%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FIACTIONRESULT%3E%3C%2FIFEEDREADER%3E%3C%2FIFEEDREADER%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22ioc-container-registration-2--resolver%22%20id%3D%22toc-hId--620960108%22%20id%3D%22toc-hId--620960108%22%3EIoC%20Container%20Registration%20%232%20%E2%80%93%20Resolver%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt's%20similar%20to%20the%20first%20approach.%20This%20time%2C%20let's%20use%20a%20resolver%20instance%20to%20get%20the%20dependency%20we%20want.%20First%20of%20all%2C%20declare%20both%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EIFeedReaderResolver%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EFeedReaderResolver%3C%2FCODE%3E.%20Keep%20an%20eye%20on%20the%20instance%20of%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EIServiceProvider%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eas%20a%20dependency.%20It's%20used%20for%20the%20built-in%20IoC%20container%20of%20ASP.NET%20Core%2C%20which%20can%20access%20to%20all%20registered%20dependencies.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20addition%20to%20that%2C%20this%20time%2C%20we%20don't%20need%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EName%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eproperty%20any%20longer%20as%20we%20use%20conventions%20to%20get%20the%20required%20instance.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20interface%20IFeedReaderResolver%0A%7B%0A%20%20%20%20IFeedReader%20Resolve(string%20name)%3B%0A%7D%0A%0Apublic%20class%20FeedReaderResolver%20%3A%20IFeedReaderResolver%0A%7B%0A%20%20%20%20private%20readonly%20IServiceProvider%20_provider%3B%0A%0A%20%20%20%20public%20FeedReaderResolver(IServiceProvider%20provider)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20this._provider%20%3D%20provider%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20IFeedReader%20Resolve(string%20name)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20type%20%3D%20Assembly.GetAssembly(typeof(FeedReaderResolver))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.GetType(%24%22%7Bname%7DFeedReader%22)%3B%0A%20%20%20%20%20%20%20%20var%20instance%20%3D%20this._provider.GetService(type)%3B%0A%0A%20%20%20%20%20%20%20%20return%20instance%20as%20IFeedReader%3B%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAfter%20that%2C%20update%26nbsp%3B%3CCODE%20class%3D%22shiki-inline%22%3EConfigure()%3C%2FCODE%3E%26nbsp%3Bon%26nbsp%3B%3CCODE%20class%3D%22shiki-inline%22%3EStartup.cs%3C%2FCODE%3E%26nbsp%3Bagain.%20Unlike%20the%20previous%20approach%2C%20we%20registered%26nbsp%3B%3CCODE%20class%3D%22shiki-inline%22%3ExxxFeedReader%3C%2FCODE%3E%26nbsp%3Binstances%2C%20not%26nbsp%3B%3CCODE%20class%3D%22shiki-inline%22%3EIFeedReader%3C%2FCODE%3E.%20It's%20fine%2C%20though.%20The%20resolver%20sorts%20this%20out%20for%26nbsp%3B%3CCODE%20class%3D%22shiki-inline%22%3EFeedReaderHttpTrigger%3C%2FCODE%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20override%20void%20Configure(IFunctionsHostBuilder%20builder)%0A%7B%0A%20%20%20%20builder.Services.AddTransient%3CBLOGFEEDREADER%3E()%3B%0A%20%20%20%20builder.Services.AddTransient%3CPODCASTFEEDREADER%3E()%3B%0A%20%20%20%20builder.Services.AddTransient%3CYOUTUBEFEEDREADER%3E()%3B%0A%0A%20%20%20%20builder.Services.AddTransient%3CIFEEDREADERRESOLVER%3E()%3B%0A%7D%3C%2FIFEEDREADERRESOLVER%3E%3C%2FYOUTUBEFEEDREADER%3E%3C%2FPODCASTFEEDREADER%3E%3C%2FBLOGFEEDREADER%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EUpdate%20the%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EFeedReaderHttpTrigger%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bclass%20like%20below.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20class%20FeedReaderHttpTrigger%0A%7B%0A%20%20%20%20private%20readonly%20IFeedReader%20_reader%3B%0A%0A%20%20%20%20public%20FeedReaderHttpTrigger(IFeedReaderResolver%20resolver)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20this._reader%20%3D%20resolver.Resolve(%22Blog%22)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%5BFunctionName(nameof(FeedReaderHttpTrigger.GetFeedItemAsync))%5D%0A%20%20%20%20public%20async%20Task%3CIACTIONRESULT%3E%20GetFeedItemAsync(%0A%20%20%20%20%20%20%20%20%5BHttpTrigger(AuthorizationLevel.Function%2C%20%22post%22%2C%20Route%20%3D%20%22feeds%2Fitem%22)%5D%20HttpRequest%20req%2C%0A%20%20%20%20%20%20%20%20ILogger%20log)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20title%20%3D%20this._reader.GetSingleFeedTitle()%3B%0A%0A%20%20%20%20%20%20%20%20return%20new%20OkObjectResult(title)%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FIACTIONRESULT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22ioc-container-registration-3--resolver--factory-method-pattern%22%20id%3D%22toc-hId-1866552725%22%20id%3D%22toc-hId-1866552725%22%3EIoC%20Container%20Registration%20%233%20%E2%80%93%20Resolver%20%2B%20Factory%20Method%20Pattern%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20slightly%20modify%20the%20resolver%20that%20uses%20the%20factory%20method%20pattern.%20After%20this%20modification%2C%20it%20removes%20the%20dependency%20on%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EIServiceProvider%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Einstance.%20Instead%2C%20it%20creates%20the%20required%20instance%20by%20using%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EActivator.CreateInstance()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20class%20FeedReaderResolver%20%3A%20IFeedReaderResolver%0A%7B%0A%20%20%20%20public%20IFeedReader%20Resolve(string%20name)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20type%20%3D%20Assembly.GetAssembly(typeof(FeedReaderResolver))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.GetType(%24%22%7Bname%7DFeedReader%22)%3B%0A%20%20%20%20%20%20%20%20var%20instance%20%3D%20Activator.CreateInstance(type)%3B%0A%0A%20%20%20%20%20%20%20%20return%20instance%20as%20IFeedReader%3B%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIf%20we%20implement%20the%20resolver%20class%20in%20this%20way%2C%20we%20don't%20need%20to%20register%20all%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3ExxxFeedReader%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bclasses%20to%20the%20IoC%20container%2C%20but%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EIFeedReaderResolver%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bwould%20be%20sufficient.%20By%20the%20way%2C%20make%20sure%20that%20all%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3ExxxFeedReader%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Binstances%20cannot%20be%20used%20as%20a%20singleton%20if%20we%20take%20this%20approach.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20override%20void%20Configure(IFunctionsHostBuilder%20builder)%0A%7B%0A%20%20%20%20builder.Services.AddTransient%3CIFEEDREADERRESOLVER%3E()%3B%0A%7D%3C%2FIFEEDREADERRESOLVER%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22ioc-container-registration-4--explicit-delegates%22%20id%3D%22toc-hId-59098262%22%20id%3D%22toc-hId-59098262%22%3EIoC%20Container%20Registration%20%234%20%E2%80%93%20Explicit%20Delegates%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20can%20replace%20the%20resolver%20with%20an%20explicit%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22delegates%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fdotnet%2Fcsharp%2Fprogramming-guide%2Fdelegates%2F%3FWT.mc_id%3Dappsonazure-techcommunity-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Edelegates%3C%2FA%3E.%20Let's%20have%20a%20look%20at%20the%20code%20below.%20Within%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EStartup.cs%3C%2FCODE%3E%2C%20declare%20a%20delegate%20just%20outside%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EStartup%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eclass.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20delegate%20IFeedReader%20FeedReaderDelegate(string%20name)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThen%2C%20update%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EConfigure()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Blike%20below.%20As%20we%20only%20declared%20the%20delegate%2C%20its%20actual%20implementation%20goes%20here.%20The%20implementation%20logic%20is%20not%20that%20different%20from%20the%20previous%20approach.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20override%20void%20Configure(IFunctionsHostBuilder%20builder)%0A%7B%0A%20%20%20%20builder.Services.AddTransient%3CBLOGFEEDREADER%3E()%3B%0A%20%20%20%20builder.Services.AddTransient%3CPODCASTFEEDREADER%3E()%3B%0A%20%20%20%20builder.Services.AddTransient%3CYOUTUBEFEEDREADER%3E()%3B%0A%0A%20%20%20%20builder.Services.AddTransient%3CFEEDREADERDELEGATE%3E(provider%20%3D%26gt%3B%20name%20%3D%26gt%3B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20type%20%3D%20Assembly.GetAssembly(typeof(FeedReaderResolver))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.GetType(%24%22FeedReaders.%7Bname%7DFeedReader%22)%3B%0A%20%20%20%20%20%20%20%20var%20instance%20%3D%20provider.GetService(type)%3B%0A%0A%20%20%20%20%20%20%20%20return%20instance%20as%20IFeedReader%3B%0A%20%20%20%20%7D)%3B%0A%7D%3C%2FFEEDREADERDELEGATE%3E%3C%2FYOUTUBEFEEDREADER%3E%3C%2FPODCASTFEEDREADER%3E%3C%2FBLOGFEEDREADER%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EUpdate%20the%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EFeedReaderHttpTrigger%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bclass.%20As%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EFeedReaderDelegate%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Breturns%20the%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EIFeedReader%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Binstance%2C%20another%20method%20call%20should%20be%20performed%20through%20the%20method%20chaining.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20class%20FeedReaderHttpTrigger%0A%7B%0A%20%20%20%20private%20readonly%20FeedReaderDelegate%20_delegate%3B%0A%0A%20%20%20%20public%20FeedReaderHttpTrigger(FeedReaderDelegate%20%40delegate)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20this._delegate%20%3D%20%40delegate%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%5BFunctionName(nameof(FeedReaderHttpTrigger.GetFeedItemAsync))%5D%0A%20%20%20%20public%20async%20Task%3CIACTIONRESULT%3E%20GetFeedItemAsync(%0A%20%20%20%20%20%20%20%20%5BHttpTrigger(AuthorizationLevel.Function%2C%20%22post%22%2C%20Route%20%3D%20%22feeds%2Fitem%22)%5D%20HttpRequest%20req%2C%0A%20%20%20%20%20%20%20%20ILogger%20log)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20title%20%3D%20this._delegate(%22Blog%22).GetSingleFeedTitle()%3B%0A%0A%20%20%20%20%20%20%20%20return%20new%20OkObjectResult(title)%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FIACTIONRESULT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22ioc-container-registration-5--implicit-delegates-with-lambda-function%22%20id%3D%22toc-hId--1748356201%22%20id%3D%22toc-hId--1748356201%22%3EIoC%20Container%20Registration%20%235%20%E2%80%93%20Implicit%20Delegates%20with%20Lambda%20Function%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInstead%20of%20using%20the%20explicit%20delegate%2C%20the%20Lambda%20function%20can%20also%20be%20used%20as%20the%20implicit%20delegate.%20Let's%20modify%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EConfigure()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20like%20below.%20As%20there%20is%20no%20declaration%20of%20the%20delegate%2C%20define%20the%20Lambda%20function.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20override%20void%20Configure(IFunctionsHostBuilder%20builder)%0A%7B%0A%20%20%20%20builder.Services.AddTransient%3CBLOGFEEDREADER%3E()%3B%0A%20%20%20%20builder.Services.AddTransient%3CPODCASTFEEDREADER%3E()%3B%0A%20%20%20%20builder.Services.AddTransient%3CYOUTUBEFEEDREADER%3E()%3B%0A%0A%20%20%20%20builder.Services.AddTransient%3CFUNC%3E%3CSTRING%3E%26gt%3B(provider%20%3D%26gt%3B%20name%20%3D%26gt%3B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20type%20%3D%20Assembly.GetAssembly(typeof(FeedReaderResolver))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.GetType(%24%22FeedReaders.%7Bname%7DFeedReader%22)%3B%0A%20%20%20%20%20%20%20%20var%20instance%20%3D%20provider.GetService(type)%3B%0A%0A%20%20%20%20%20%20%20%20return%20instance%20as%20IFeedReader%3B%0A%20%20%20%20%7D)%3B%0A%7D%3C%2FSTRING%3E%3C%2FFUNC%3E%3C%2FYOUTUBEFEEDREADER%3E%3C%2FPODCASTFEEDREADER%3E%3C%2FBLOGFEEDREADER%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAs%20the%20injected%20object%20is%20the%20Lambda%20function%2C%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22shiki-inline%22%3EFeedReaderHttpTrigger%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3Bshould%20accept%20the%20Lambda%20function%20as%20a%20dependency.%20While%20it's%20closely%20similar%20to%20the%20previous%20example%2C%20it%20uses%20the%20Lambda%20function%20this%20time%20for%20dependency%20injection.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20class%20FeedReaderHttpTrigger%0A%7B%0A%20%20%20%20private%20readonly%20Func%3CSTRING%3E%20_func%3B%0A%0A%20%20%20%20public%20FeedReaderHttpTrigger(Func%3CSTRING%3E%20func)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20this._func%20%3D%20func%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%5BFunctionName(nameof(FeedReaderHttpTrigger.GetFeedItemAsync))%5D%0A%20%20%20%20public%20async%20Task%3CIACTIONRESULT%3E%20GetFeedItemAsync(%0A%20%20%20%20%20%20%20%20%5BHttpTrigger(AuthorizationLevel.Function%2C%20%22post%22%2C%20Route%20%3D%20%22feeds%2Fitem%22)%5D%20HttpRequest%20req%2C%0A%20%20%20%20%20%20%20%20ILogger%20log)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20title%20%3D%20this._func(%22Blog%22).GetSingleFeedTitle()%3B%0A%0A%20%20%20%20%20%20%20%20return%20new%20OkObjectResult(title)%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FIACTIONRESULT%3E%3C%2FSTRING%3E%3C%2FSTRING%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E---%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ESo%20far%2C%20we%20have%20discussed%20five%20different%20ways%20to%20resolve%20injected%20dependencies%20using%20the%20same%20interface%2C%20while%20building%20an%20%3CA%20title%3D%22Azure%20Functions%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-functions%2Ffunctions-overview%3FWT.mc_id%3Dappsonazure-techcommunity-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Functions%3C%2FA%3E%20application.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3EThose%20five%20approaches%20are%20very%20similar%20to%20each%20other.%20Then%2C%20which%20one%20to%20choose%3F%20Well%2C%20there's%20no%20one%20approach%20better%20than%20the%20other%20four%2C%20but%20I%20guess%20it%20would%20depend%20purely%20on%20the%20developer's%20preference.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%3CSPAN%3EThis%20post%20has%20been%20cross-posted%20to%20%3CA%20title%3D%22Go%20to%20DevKimchi%22%20href%3D%22https%3A%2F%2Fdevkimchi.com%2F2020%2F07%2F01%2F5-ways-injecting-multiple-instances-of-same-interface-on-aspnet-core%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDevKimchi%3C%2FA%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1502394%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%225-ways-injecting-multiple-instances-of-same-interface-on-aspnet-core-00%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F202783iDCA3CD574B7E5D9C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%225-ways-injecting-multiple-instances-of-same-interface-on-aspnet-core-00%22%20alt%3D%225-ways-injecting-multiple-instances-of-same-interface-on-aspnet-core-00%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EWhile%20building%20an%20-ERR%3AREF-NOT-FOUND-Azure%20Functions%20application%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E-ERR%3AREF-NOT-FOUND-setting%20an%20IoC%20container%20for%20dependency%20injection%3CSPAN%3E%26nbsp%3Bhas%20many%20benefits%20by%20comparing%20to%20just%20using%20the%20static%20classes%20and%20methods%3C%2FSPAN%3E.%20Azure%20Functions%20leverages%20the%20built-in%20IoC%20container%20featured%20by%20-ERR%3AREF-NOT-FOUND-ASP.NET%20Core%20that%20is%20easy%20to%20use%2C%20without%20having%20to%20rely%20on%20any%20third-party%20libraries.%20Throughout%20this%20post%2C%20I'm%20going%20to%20discuss%20five%20different%20ways%20to%20pick%20up%20a%20dependency%20injected%20from%20multiple%20instances%20sharing%20with%20the%20same%20interface.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1502394%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3E.NET%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

While building an Azure Functions application, setting an IoC container for dependency injection has many benefits by comparing to just using the static classes and methods. Azure Functions leverages the built-in IoC container featured by ASP.NET Core that is easy to use, without having to rely on any third-party libraries. Throughout this post, I'm going to discuss five different ways to pick up a dependency injected from multiple instances sharing with the same interface.

 

Implementing Interface and Classes

 

Let's assume that we're writing one IFeedReader interface and three different classes, BlogFeedReader, PodcastFeedReader and YouTubeFeedReader implementing the interface. There's nothing fancy.

 

 

public interface IFeedReader
{
    string Name { get; }
    string GetSingleFeedTitle();
}

public class BlogFeedReader : IFeedReader
{
    public BlogFeedReader()
    {
        this.Name = "Blog";
    }

    public string Name { get; }

    public string GetSingleFeedTitle()
    {
        return "This is blog item 1";
    }
}

public class PodcastFeedReader : IFeedReader
{
    public PodcastFeedReader()
    {
        this.Name = "Podcast";
    }

    public string Name { get; }

    public string GetSingleFeedTitle()
    {
        return "This is audio item 1";
    }
}

public class YouTubeFeedReader : IFeedReader
{
    public YouTubeFeedReader()
    {
        this.Name = "YouTube";
    }

    public string Name { get; }

    public string GetSingleFeedTitle()
    {
        return "This is video item 1";
    }
}

 

 

IoC Container Registration #1 – Collection

 

Let's register all the classes declared above into the Configure() method in Startup.cs.

 

 

public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services.AddTransient<IFeedReader, BlogFeedReader>();
    builder.Services.AddTransient<IFeedReader, PodcastFeedReader>();
    builder.Services.AddTransient<IFeedReader, YouTubeFeedReader>();
}

 

 

By doing so, all three classes have been registered as IFeedReader instances. However, from the consumer perspective, it doesn't know which instance is appropriate to use. In this case, using a collection as IEnumerable<IFeedReader> is useful. In other words, inject a dependency of IEnumerable<IFeedReader> to the FeedReaderHttpTrigger class and filter one instance from the collection.

 

 

public class FeedReaderHttpTrigger
{
    private readonly IFeedReader _reader;

    public FeedReaderHttpTrigger(IEnumerable<IFeedReader> readers)
    {
        this._reader = readers.SingleOrDefault(p => p.Name == "Blog");
    }

    [FunctionName(nameof(FeedReaderHttpTrigger.GetFeedItemAsync))]
    public async Task<IActionResult> GetFeedItemAsync(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "feeds/item")] HttpRequest req,
        ILogger log)
    {
        var title = this._reader.GetSingleFeedTitle();

        return new OkObjectResult(title);
    }
}

 

 

It's one way to use the collection as a dependency. The other way to use the collection is to use a loop. It's useful when we implement either a Visitor Pattern or Iterator Pattern.

 

 

public class FeedReaderHttpTrigger
{
    private readonly IEnumerable<IFeedReader> _readers;

    public FeedReaderHttpTrigger(IEnumerable<IFeedReader> readers)
    {
        this._readers = readers;
    }

    [FunctionName(nameof(FeedReaderHttpTrigger.GetFeedItemAsync))]
    public async Task<IActionResult> GetFeedItemAsync(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "feeds/item")] HttpRequest req,
        ILogger log)
    {
        var title = default(string);
        foreach (reader in this._readers)
        {
            if (reader.Name != "Blog")
            {
                continue;
            }

            title = reader.GetSingleFeedTitle();
        }

        return new OkObjectResult(title);
    }
}

 

 

IoC Container Registration #2 – Resolver

 

It's similar to the first approach. This time, let's use a resolver instance to get the dependency we want. First of all, declare both IFeedReaderResolver and FeedReaderResolver. Keep an eye on the instance of IServiceProvider as a dependency. It's used for the built-in IoC container of ASP.NET Core, which can access to all registered dependencies.

 

In addition to that, this time, we don't need the Name property any longer as we use conventions to get the required instance.

 

 

public interface IFeedReaderResolver
{
    IFeedReader Resolve(string name);
}

public class FeedReaderResolver : IFeedReaderResolver
{
    private readonly IServiceProvider _provider;

    public FeedReaderResolver(IServiceProvider provider)
    {
        this._provider = provider;
    }

    public IFeedReader Resolve(string name)
    {
        var type = Assembly.GetAssembly(typeof(FeedReaderResolver))
                           .GetType($"{name}FeedReader");
        var instance = this._provider.GetService(type);

        return instance as IFeedReader;
    }
}

 

 

After that, update Configure() on Startup.cs again. Unlike the previous approach, we registered xxxFeedReader instances, not IFeedReader. It's fine, though. The resolver sorts this out for FeedReaderHttpTrigger.

 

 

public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services.AddTransient<BlogFeedReader>();
    builder.Services.AddTransient<PodcastFeedReader>();
    builder.Services.AddTransient<YouTubeFeedReader>();

    builder.Services.AddTransient<IFeedReaderResolver, FeedReaderResolver>();
}

 

 

Update the FeedReaderHttpTrigger class like below.

 

 

public class FeedReaderHttpTrigger
{
    private readonly IFeedReader _reader;

    public FeedReaderHttpTrigger(IFeedReaderResolver resolver)
    {
        this._reader = resolver.Resolve("Blog");
    }

    [FunctionName(nameof(FeedReaderHttpTrigger.GetFeedItemAsync))]
    public async Task<IActionResult> GetFeedItemAsync(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "feeds/item")] HttpRequest req,
        ILogger log)
    {
        var title = this._reader.GetSingleFeedTitle();

        return new OkObjectResult(title);
    }
}

 

 

IoC Container Registration #3 – Resolver + Factory Method Pattern

 

Let's slightly modify the resolver that uses the factory method pattern. After this modification, it removes the dependency on the IServiceProvider instance. Instead, it creates the required instance by using the Activator.CreateInstance() method.

 

 

public class FeedReaderResolver : IFeedReaderResolver
{
    public IFeedReader Resolve(string name)
    {
        var type = Assembly.GetAssembly(typeof(FeedReaderResolver))
                           .GetType($"{name}FeedReader");
        var instance = Activator.CreateInstance(type);

        return instance as IFeedReader;
    }
}

 

 

If we implement the resolver class in this way, we don't need to register all xxxFeedReader classes to the IoC container, but IFeedReaderResolver would be sufficient. By the way, make sure that all xxxFeedReader instances cannot be used as a singleton if we take this approach.

 

 

public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services.AddTransient<IFeedReaderResolver, FeedReaderResolver>();
}

 

 

IoC Container Registration #4 – Explicit Delegates

 

We can replace the resolver with an explicit delegates. Let's have a look at the code below. Within Startup.cs, declare a delegate just outside the Startup class.

 

 

public delegate IFeedReader FeedReaderDelegate(string name);

 

 

Then, update Configure() like below. As we only declared the delegate, its actual implementation goes here. The implementation logic is not that different from the previous approach.

 

 

public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services.AddTransient<BlogFeedReader>();
    builder.Services.AddTransient<PodcastFeedReader>();
    builder.Services.AddTransient<YouTubeFeedReader>();

    builder.Services.AddTransient<FeedReaderDelegate>(provider => name =>
    {
        var type = Assembly.GetAssembly(typeof(FeedReaderResolver))
                           .GetType($"FeedReaders.{name}FeedReader");
        var instance = provider.GetService(type);

        return instance as IFeedReader;
    });
}

 

 

Update the FeedReaderHttpTrigger class. As FeedReaderDelegate returns the IFeedReader instance, another method call should be performed through the method chaining.

 

 

public class FeedReaderHttpTrigger
{
    private readonly FeedReaderDelegate _delegate;

    public FeedReaderHttpTrigger(FeedReaderDelegate @delegate)
    {
        this._delegate = @delegate;
    }

    [FunctionName(nameof(FeedReaderHttpTrigger.GetFeedItemAsync))]
    public async Task<IActionResult> GetFeedItemAsync(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "feeds/item")] HttpRequest req,
        ILogger log)
    {
        var title = this._delegate("Blog").GetSingleFeedTitle();

        return new OkObjectResult(title);
    }
}

 

 

IoC Container Registration #5 – Implicit Delegates with Lambda Function

 

Instead of using the explicit delegate, the Lambda function can also be used as the implicit delegate. Let's modify the Configure() method like below. As there is no declaration of the delegate, define the Lambda function.

 

 

public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services.AddTransient<BlogFeedReader>();
    builder.Services.AddTransient<PodcastFeedReader>();
    builder.Services.AddTransient<YouTubeFeedReader>();

    builder.Services.AddTransient<Func<string, IFeedReader>>(provider => name =>
    {
        var type = Assembly.GetAssembly(typeof(FeedReaderResolver))
                           .GetType($"FeedReaders.{name}FeedReader");
        var instance = provider.GetService(type);

        return instance as IFeedReader;
    });
}

 

 

As the injected object is the Lambda function, FeedReaderHttpTrigger should accept the Lambda function as a dependency. While it's closely similar to the previous example, it uses the Lambda function this time for dependency injection.

 

 

public class FeedReaderHttpTrigger
{
    private readonly Func<string, IFeedReader> _func;

    public FeedReaderHttpTrigger(Func<string, IFeedReader> func)
    {
        this._func = func;
    }

    [FunctionName(nameof(FeedReaderHttpTrigger.GetFeedItemAsync))]
    public async Task<IActionResult> GetFeedItemAsync(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "feeds/item")] HttpRequest req,
        ILogger log)
    {
        var title = this._func("Blog").GetSingleFeedTitle();

        return new OkObjectResult(title);
    }
}

 

 

---

 

So far, we have discussed five different ways to resolve injected dependencies using the same interface, while building an Azure Functions application. Those five approaches are very similar to each other. Then, which one to choose? Well, there's no one approach better than the other four, but I guess it would depend purely on the developer's preference.

 

This post has been cross-posted to DevKimchi.