You can use this example to understand the concept of event and delegate. It is a simple example of publisher and subscriber.
In this example, the publisher is a class called Publisher
and the subscriber is a class called Subscriber
. The publisher class
exposes an event called OnPublish
and the subscriber class has a method called Subscribe
that is subscribed to the publisher's
event.
When the publisher class invokes its event, all the subscribed methods are invoked.
- Download the source code
- Open the solution file in Visual Studio
- Compile the solution
- Run the application
- run the API project for the API
- run the console project for the console application
Try to understand the code and play with it. You can also add more subscribers to the publisher's event and see how it works.
It is not good for production. you can use this example for learning and understanding the concept of pub sub. I would prefer mediator pattern for production. you can find the mediator pattern example in other repository.