- Print
- DarkLight
- PDF
Multiple instances of same interface in Azure Functions
- Print
- DarkLight
- PDF
There can be cases while developing Azure Functions where you might want to use the different implementations of an interface to achieve different functionality e.g., you might be writing a serverless notification microservice that is responsible for sending SMS, and WhatsApp notifications for the order placed by the customer.
.NET core provides a simple dependency injection pattern, which you can use for this scenario.
Start by adding an interface for the notification service as shown below
For sake of simplicity the SMS and WhatsApp implementation look like below
You can now register both the implementations in the Startup class as shown in following screenshot
The dependency injection will be a bit different, and you will have to use the IEnumerable
The implementations then can be simply invoked as shown
The function code can iterate through the collection of the implementations and the result will be as shown below:
This nifty pattern can be used with any number of implementations of the interface as required.