Events and Delegates

Events and Delegates

Events

Events are a mechanism for communication between objects. When something happens inside an object, it can notify other objects about it.

Why Do We Need Events?

Events are essential for building loosely coupled applications. They allow us to extend the application easily without changing or breaking existing functionalities.

Let’s break it down with a clear example:

In the example above, we are encoding a video and then sending an email to someone. There is nothing inherently wrong with this approach, but what if we decide to send a text message as well? In this case, we would need to add an additional line of code.

The problem with this approach is that when we add a new option to send a notification, we are forced to change the Encode() method. This change requires recompiling the Encode() method, the container class, and all dependent classes.

To solve this problem, we can make the Encode() method act as a publisher or event sender. It will send an event to subscribers, such as MailService and MessageService, which will act as subscribers or event receivers.

If we implement this event mechanism, the code will look like the code below

The purpose of the OnVideoEncoded() method is to notify the subscribers by sending messages to them—in other words, by invoking their methods. But how does VideoEncoder know which methods to call? We need an agreement or contract between the publisher and the subscribers.

Subscribers need to have an event handler, which is a method with the following signature:


The publisher knows nothing about the subscribers; all it knows is a method signature. So, how does VideoEncoder know which method to call? This is where a delegate comes in.

Here's the optimized version of the next text block:


Delegates

Delegates act as an agreement or contract between the publisher and the subscriber, determining the signature of the event handler method in the subscriber.

Returning to our example, if we want VideoEncoder to notify anyone who needs to be notified and publish an event, we need to follow three steps:

  1. Define a delegate.
  2. Define an event based on the delegate.
  3. Raise the event.

The code will look like this:

Now, we will create the subscribers, we should mention that they have same delegate signature like return type and parameters.

Then, we need to add the subscribers to the event handler like this:

And the result is:


要查看或添加评论,请登录

Ali Shakkouf的更多文章

  • DbContext Management

    DbContext Management

    To manage multiple DbContexts dynamically, I implemented a factory-based approach. The interface allowed me to create…

  • Caching patterns and techniques

    Caching patterns and techniques

    Caching patterns are strategies used to optimize data retrieval and improve application performance by temporarily…

  • Nuget package to deal with images

    Nuget package to deal with images

    NuGet Package: ImageHelper404 With ImageHelper404 package, working with images is now faster, more efficient, and…

    2 条评论
  • Nuget package to deal with currencies

    Nuget package to deal with currencies

    NuGet Package: CurrencyHelper This package offers a range of utilities for dealing with currencies, including symbol…

    3 条评论
  • Effortless Data Seeding in .NET 8 using Reflection!

    Effortless Data Seeding in .NET 8 using Reflection!

    I’ve been working on a project using ASP.NET Core and decided to handle data seeding with a more scalable and…

    5 条评论
  • What is Caching?

    What is Caching?

    At its core, caching is a technique used to store frequently accessed data in a temporary storage area (the cache) to…

    2 条评论
  • Elevating Data Integrity with Enhanced Auditing Solutions ??

    Elevating Data Integrity with Enhanced Auditing Solutions ??

    In the realm of data management, the ability to track and audit changes effectively is not just a best practice, it's…

    6 条评论
  • Unlocking the Power of Events and Delegates in C#

    Unlocking the Power of Events and Delegates in C#

    Hello again! I'm excited to return with a nice, clear example that will help you understand the power of events and…

  • Dapper

    Dapper

    Dapper is a micro-ORM (object-relational mapping) library for .NET and .

    2 条评论
  • Isolation levels and violations

    Isolation levels and violations

    In the realm of database transactions, isolation levels play a crucial role in ensuring the consistency and reliability…

社区洞察

其他会员也浏览了