0% found this document useful (0 votes)
7 views

Curs Delegates

The document discusses events and delegates in .NET. Events allow objects to notify other objects of occurrences, while delegates allow methods to be passed as parameters. The document provides code examples of implementing events to notify subscribers when an order is processed.

Uploaded by

Umika 69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Curs Delegates

The document discusses events and delegates in .NET. Events allow objects to notify other objects of occurrences, while delegates allow methods to be passed as parameters. The document provides code examples of implementing events to notify subscribers when an order is processed.

Uploaded by

Umika 69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Events and Delegates

What are the events? What are the delegates?


Events
- A communication between objects mechanism
- Very usefull for extending applications
- Used for generating Loosely Coupled Apps

Delegates
- References to methods with a particular param list and return type.
- Associate instance with methods - compatible signature and return type
Why do we need them?
Events -> used to notify other classes or objects when something of
interest occurs.

The class that sends the event is called the publisher


The classes that receives the event are called subscribers
Events properties
• The publisher -> event is raised;
The subscribers -> action in response to the event.

• Event -> multiple subscribers


• Subscriber -> multiple events from multiple publishers.

• Events with no subscribers are never raised.


• Events are used to signal user actions such as button clicks or menu
selections in graphical user interfaces.
Delegates properties
Delegates have the following properties:
• Delegates are similar to C++ function pointers
• Delegates allow methods to be passed as parameters.
• Delegates can be used to define callback methods.
• Delegates can be chained together; for example, multiple methods
can be called on a single event.
• Methods don't have to match the delegate type exactly
Problem to solve?
public class OrderProcessor
{
public void Process(Order order)
{
//processing logic
//…

_mailService.Send(new Mail());
}
}
Problem to solve?
Recompile:
- Process method
public class OrderProcessor - OrderProcessor class
{ - All classed that depends on the class
Redeploy the application
public void Process(Order order)
{
//processing logic
//…

_mailService.Send(new Mail());

_messageService.Send(new Text());
}
}
OrderProcessed

OrderProcessor MailService

Publisher / event sender Subscriber / event receiver


OrderProcessed

OrderProcessor MailService

Publisher / event sender Subscriber / event receiver

MessageService
Problem to solve?
public class OrderProcessor
{
public void Process(Order order)
{
//processing logic
//…

OnOrderProcessed();

}
}
public void OnOrderProcessed(object source, EventArgs e)
{
}

public void OnOrderProcessed(object


Event handler source, EventArgs e)
Code example – Order class
public class Order
{
public string OrderTitle { get; set;}
public string OrderNumber { get; set;}
}
Code example – OrderProcessor class
public class OrderProcessor
{
public void Process(Order order)
{
Console.WriteLine(“Command is being processed”);
Thread.Sleep(3000);
}
}
Code example – Program class
class Program
{
static void Main(string[] args)
{
var order = new Order(){OrderTitle = ”FirstOrder”,OrderNumber=”1”};
var orderProcessor = new OrderProcessor();

orderProcessor.Process(order);
}
}
Code example – Implement event
public class OrderProcessor
{
//1 - define a delegate
//2 - define an event based on the delegate
//3 - raise the event
public void Process(Order order)
{
Console.WriteLine(“Command is being processed”);
Thread.Sleep(3000);
}
}
Code example – Implement event
public class OrderProcessor
{
//1 - define a delegate
public delegate void OrderProcessedEventHandler(object source, EventArgs args);
//2 - define an event based on the delegate
//3 - raise the event
public void Process(Order order)
{
Console.WriteLine(“Command is being processed”);
Thread.Sleep(3000);
}
}
Code example – Define event based on the delegate
public class OrderProcessor
{
//1 - define a delegate Signature of method in subscriber
public delegate void OrderProcessedEventHandler(object source, EventArgs args);
//2 - define an event based on the delegate
public event OrderProcessedEventHandler OrderProcessed;
//3 - raise the event

public void Process(Order order)


{
Console.WriteLine(“Command is being processed”);
Thread.Sleep(3000);
}
}
Code example – Raise the event
public class OrderProcessor
{
//1 - define a delegate
public delegate void OrderProcessedEventHandler(object source, EventArgs args);
//2 - define an event based on the delegate
public event OrderProcessedEventHandler OrderProcessed;
//3 - raise the event

public void Process(Order order)


{
Console.WriteLine(“Command is being processed”);
Thread.Sleep(3000);
OnOrderProcessed();
}
Protected virtual void OnOrderProcessed(Order order){ }

}
Code example – notify the subscribers
public class OrderProcessor
{
Protected virtual void OnOrderProcessed(Order order)
{
if (OrderProcessed != null)
OrderProcessed(this, EventArgs.Empty);
}
So, if we have notifiers we simply call them
}
Code example – notify the subscribers
class Program
{
static void Main(string[] args)
{ }
}

public class MailService


{
public void OnOrderProcessed(object source, EventArgs e)
{
Console.WriteLine(”MailService: Sending the email to the administrator…”);
}

}
Code example – notify the subscribers
class Program
{
static void Main(string[] args)
{
var order = new Order(){OrderTitle = ”FirstOrder”,OrderNumber=”1”};
var orderProcessor = new OrderProcessor(); //publisher

var mailService = new MailService(); //subscriber

orderProcessor. OrderProcessed += mailService.OnOrderProcessed; //no call to the method we reference (point to the method)
orderProcessor.Process(order);
}
}

public class MailService


{ public void OnOrderProcessed(object source, EventArgs e)
{Console.WriteLine(”MailService: Sending the email to the administrator…”);}}
Result
Code example – create the message service method
class Program
{
static void Main(string[] args)
{ }
}

public class MessageService


{
public void OnOrderProcessed(object source, EventArgs e)
{
Console.WriteLine(”MessageService: Sending the SMS message to the administrator…”);
}

}
Code example – add the new subscriber
class Program
{
static void Main(string[] args)
{
var order = new Order(){OrderTitle = ”FirstOrder”,OrderNumber=”1”};
var orderProcessor = new OrderProcessor(); //publisher

var mailService = new MailService(); //subscriber


var messageService = new MessageService(); //subscriber

orderProcessor. OrderProcessed += mailService.OnOrderProcessed; //no call to the method we reference


(point to the method)
orderProcessor. OrderProcessed += messageService.OnOrderProcessed;
orderProcessor.Process(order);
}
}
Result – 2 subscribers
Further extensions
//1 - define a delegate
public delegate void OrderProcessedEventHandler(object source,
EventArgs args);

public class OrderEventArgs : EventArgs


{
public string Id { get; set; }
public string Title { get; set; }
}
Further extensions
//1 - define a delegate
public delegate void OrderProcessedEventHandler(object source,
OrderEventArgs args);

Protected virtual void OnOrderProcessed(Order order) protected virtual void OnOrderProcessed(Order order)
{ {
if (OrderProcessed != null) if (OrderProcessed != null)
OrderProcessed(this, EventArgs.Empty); OrderProcessed(this, new OrderEventArgs() {
} Id = order.OrderNumber, Title = order.OrderTitle });
}
Further extensions
public class MailService
{
public void OnOrderProcessed(object source, OrderEventArgs e)
{
Console.WriteLine("MailService: Sending the email to the administrator for OrderID="
+ e.Id + "with title: "+e.Title);
}

public class MessageService


{
public void OnOrderProcessed(object source, OrderEventArgs e)
{
Console.WriteLine("MessageService: Sending the SMS text to the administrator for OrderID=" +
e.Id + "with title: " + e.Title);
}
}
Result

You might also like