I would like to explain one of the important usage of Delegates and Events.
Delegates
In most of the blogs, you can find the answer that Delegates are similar to function pointers in C/C++, and we can use Delegate to call methods at runtime.
I will explain one of the real need of Delegates: First, we need to understand what the Publisher subscriber model is.
Delegates
In most of the blogs, you can find the answer that Delegates are similar to function pointers in C/C++, and we can use Delegate to call methods at runtime.
I will explain one of the real need of Delegates: First, we need to understand what the Publisher subscriber model is.
"Publish/subscribe messaging, or pub/sub messaging, is a form of asynchronous service-to-service communication used in serverless and microservices architectures. In a pub/sub model, any message published to a topic is immediately received by all of the subscribers to the topic. Pub/sub messaging can be used to enable event-driven architectures, or to decouple applications in order to increase performance, reliability, and scalability".
Let's take one example and understand more about Publisher/Subscriber model and Delegates.
I have an ApplicationProcessing Service which is responsible to process the Application and my current requirement is when the Application processing is finished we should send an email to the admin team/IT ops team.
Let's look at my Services classes.
I have an ApplicationProcessing Service which is responsible to process the Application and my current requirement is when the Application processing is finished we should send an email to the admin team/IT ops team.
Let's look at my Services classes.
ApplicationProcessingService.cs
- public class ApplicationProcessingService : IAppProcessingService
- {
- private readonly IEmailService _emailService;
- public ApplicationProcessingService(IEmailService emailService)
- {
- _emailService = emailService;
- }
- public void ProcessApplications(IApplication application)
- {
- Thread.Sleep(2000);
- //My application processing code goes here --- Adding some Thread.Sleep
- _emailService.SendEmail(application);
- }
- }
My EmailSerice.cs Service
- public partial class EmailService : IEmailService
- {
- public void SendEmail(IApplication application)
- {
- Console.WriteLine("Sending Email to " + application.ApplicantEmail);
- }
- }
So far everything is fantastic. Now, we have another change request saying, we need to add extra notification system to our application processing (it can be Text Message/ some other notification system).
What will you do?
Are you going to Inject ITextMessageService and update ApplicationProcessing Service to call TextMessageService provider?
This is not a good practice, we have to focus now on "O" in solid priniciples.
What will you do?
Are you going to Inject ITextMessageService and update ApplicationProcessing Service to call TextMessageService provider?
This is not a good practice, we have to focus now on "O" in solid priniciples.
"Software entities … should be open for extension, but closed for modification."
Here we need to think about Delegates
As per the change request, we definitely need another notification provider class, lets build TextMessageService
- public class TextMessageService : ITextMessageService
- {
- public void SendMessage(IApplication application)
- {
- Console.WriteLine("Sending Text Message to " + application.ApplicantPhone);
- }
- }
Modify our AppProcessingService (I have created another class with different name for this demo) and add a delegate TransmitMessageToconsumers(IApplication application)
- public class ApplicationProcessingServiceWithDelegates : IAppProcessingService
- {
- public delegate void TransmitMessageToConsumers(IApplication application);
- public TransmitMessageToConsumers TransmitMessageToConsumerDelegate = null;
- public void ProcessApplications(IApplication application)
- {
- Thread.Sleep(1000);
- Console.WriteLine("Application processing is done for the Applicant " + application.ApplicantName);
- TransmitMessageToConsumerDelegate?.Invoke(application);
- }
- }
In our client application I need to make a few updates to use this Delegate.
Join the conversation! Your thoughts help the community grow.