Lädt...


🔧 C# Advanced: Subscribing to Events


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Meta Description:

Learn how to handle events in C# using a stock management system example. This article covers the fundamentals of subscribing to events, providing practical examples and assignments at various difficulty levels.

Introduction

Events in C# provide an efficient way for objects to communicate without being tightly coupled. They follow the publish-subscribe pattern, where one object (the publisher) notifies other objects (subscribers) when a specific action occurs. In this article, we’ll walk through a practical example of event handling using a stock management system and provide assignments to help you apply the concept at various difficulty levels.

1. What Are Events in C#?

Events in C# are built on delegates and allow for flexible communication between objects. They are particularly useful in scenarios where one object needs to notify other objects that something important has happened.

In our stock management example, the system will notify subscribers when stock levels fall below a certain threshold, triggering actions like restocking and notifying the supplier.

2. Stock Management System Example

Let’s build a simple stock management system that monitors product inventory. When the stock of an item falls below a certain level (5 units), it triggers an event to notify the WarehouseManager and SupplierService to take action.

Step 1: Define the Event in the Inventory Manager

We will define a StockLow event in the InventoryManager class. This event is raised when the stock of an item drops below 5 units.

using System;

public class StockEventArgs : EventArgs
{
    public string ItemName { get; set; }
    public int CurrentStock { get; set; }
}

public class InventoryManager
{
    public event EventHandler<StockEventArgs> StockLow;

    private int _stockLevel;
    public string ItemName { get; set; }

    public InventoryManager(string itemName, int initialStock)
    {
        ItemName = itemName;
        _stockLevel = initialStock;
    }

    public void ProcessSale(int quantity)
    {
        _stockLevel -= quantity;
        Console.WriteLine($"{quantity} {ItemName}(s) sold. Current stock: {_stockLevel}");

        if (_stockLevel < 5)
        {
            OnStockLow();
        }
    }

    protected virtual void OnStockLow()
    {
        StockLow?.Invoke(this, new StockEventArgs { ItemName = this.ItemName, CurrentStock = this._stockLevel });
    }
}

Explanation:

  • The InventoryManager class tracks the stock of a product.
  • The StockLow event is triggered when the stock falls below the threshold of 5 units.
  • The StockEventArgs class is used to pass the item name and current stock to the event subscribers.

Step 2: Subscribe to the Event

Next, we create a WarehouseManager and a SupplierService, both of which will subscribe to the StockLow event. When the stock level falls below the threshold, they take appropriate actions such as restocking or notifying the supplier.

public class WarehouseManager
{
    public void Restock(object sender, StockEventArgs e)
    {
        Console.WriteLine($"Warehouse notified: Restock {e.ItemName} (Current stock: {e.CurrentStock})");
    }
}

public class SupplierService
{
    public void NotifySupplier(object sender, StockEventArgs e)
    {
        Console.WriteLine($"Supplier notified to replenish stock for {e.ItemName}.");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        InventoryManager inventory = new InventoryManager("Laptop", 10);
        WarehouseManager warehouse = new WarehouseManager();
        SupplierService supplier = new SupplierService();

        // Subscribe to the StockLow event
        inventory.StockLow += warehouse.Restock;
        inventory.StockLow += supplier.NotifySupplier;

        // Simulate sales that reduce stock
        inventory.ProcessSale(3);  // Stock = 7, no event triggered
        inventory.ProcessSale(4);  // Stock = 3, event triggered
    }
}

Explanation:

  • The WarehouseManager and SupplierService subscribe to the StockLow event.
  • When the stock drops below 5, both subscribers are notified, and actions are triggered (restocking and supplier notification).

3. How Events Work in This System

In this stock management system, the InventoryManager acts as the publisher. When the stock level falls below a certain threshold, it raises the StockLow event. The WarehouseManager and SupplierService are subscribers that respond to the event by restocking and notifying suppliers.

Publisher: InventoryManager

  • Raises the StockLow event when stock levels drop.

Subscribers: WarehouseManager and SupplierService

  • Handle the event by performing actions like restocking and notifying suppliers.

4. Important Event Patterns in C#

Here are some best practices to keep in mind when working with events in C#:

  1. Use EventHandler: Always use EventHandler or EventHandler<T> to follow C# conventions for event signatures.
  2. Unsubscribe from Events: Ensure that subscribers properly unsubscribe from events when no longer needed to avoid memory leaks.
  3. Sequential Execution: Event handlers are executed in the order they were added unless explicitly handled asynchronously.

Full Code: Stock Management System Using Events in C

using System;

public class StockEventArgs : EventArgs
{
    public string ItemName { get; set; }
    public int CurrentStock { get; set; }
}

public class InventoryManager
{
    public event EventHandler<StockEventArgs> StockLow;

    private int _stockLevel;
    public string ItemName { get; set; }

    public InventoryManager(string itemName, int initialStock)
    {
        ItemName = itemName;
        _stockLevel = initialStock;
    }

    public void ProcessSale(int quantity)
    {
        _stockLevel -= quantity;
        Console.WriteLine($"{quantity} {ItemName}(s) sold. Current stock: {_stockLevel}");

        if (_stockLevel < 5)
        {
            OnStockLow();
        }
    }

    protected virtual void OnStockLow()
    {
        StockLow?.Invoke(this, new StockEventArgs { ItemName = this.ItemName, CurrentStock = this._stockLevel });
    }
}

public class WarehouseManager
{
    public void Restock(object sender, StockEventArgs e)
    {
        Console.WriteLine($"Warehouse notified: Restock {e.ItemName} (Current stock: {e.CurrentStock})");
    }
}

public class SupplierService
{
    public void NotifySupplier(object sender, StockEventArgs e)
    {
        Console.WriteLine($"Supplier notified to replenish stock for {e.ItemName}.");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        InventoryManager inventory = new InventoryManager("Laptop", 10);
        WarehouseManager warehouse = new WarehouseManager();
        SupplierService supplier = new SupplierService();

        // Subscribe to the StockLow event
        inventory.StockLow += warehouse.Restock;
        inventory.StockLow += supplier.NotifySupplier;

        // Simulate sales that reduce stock
        inventory.ProcessSale(3);  // Stock = 7, no event triggered
        inventory.ProcessSale(4);  // Stock = 3, event triggered
    }
}

5. Assignments to Practice Events in C#

Here are three assignments to help you apply what you’ve learned:

Easy Level:

Modify the InventoryManager class to raise a new event called StockFull when the stock goes above 50 units after a restock. Subscribe to this event in both the WarehouseManager and SupplierService classes.

Hint: Define and raise the StockFull event similarly to the StockLow event.

Medium Level:

Modify the program to track multiple products in the InventoryManager. Create instances for "Laptops" and "Tablets," each with its own stock level. Ensure both can independently trigger stock alerts.

Hint: Use a list or dictionary to manage different products and their stock levels.

Difficult Level:

Make event handling asynchronous. Modify the OnStockLow method to execute event handlers asynchronously using Task.Run. Simulate delays in the Restock and NotifySupplier methods using Task.Delay() to mimic real-world processing times.

Hint: Use async and await to handle tasks concurrently.

Conclusion

Events are a key mechanism for enabling communication between different parts of your application. They allow for loose coupling between components, making your code easier to maintain and extend. The stock management system example provides a clear illustration of how to use events effectively in C#. By completing the provided assignments, you will gain hands-on experience and a deeper understanding of event handling in real-world scenarios.

...

🔧 C# Advanced: Subscribing to Events


📈 43.96 Punkte
🔧 Programmierung

📰 How do I hide my IP address while online without subscribing to a service?


📈 27.42 Punkte
📰 IT Security Nachrichten

🕵️ WAPDropper – Android Malware Subscribing Victims To Premium Services By Telecom Companies


📈 27.42 Punkte
🕵️ Hacking

📰 Sites Trick Users Into Subscribing to Browser Notification Spam


📈 27.42 Punkte
📰 IT Security Nachrichten

📰 Subscribing to Our Podcast


📈 27.42 Punkte
📰 IT Security Nachrichten

🍏 How To Use the Music App On Your Mac Without Subscribing To Apple Music


📈 27.42 Punkte
🍏 iOS / Mac OS

🔧 (MQTT Series) Part 3 - Publishing Subscribing and Topics


📈 27.42 Punkte
🔧 Programmierung

🍏 ‘Slow Horses’ alone justifies subscribing to Apple TV+


📈 27.42 Punkte
🍏 iOS / Mac OS

🍏 How subscribing to SaaS puts you back in control


📈 27.42 Punkte
🍏 iOS / Mac OS

🕵️ CVE-2018-25076 | Events Extension on BigTree classes/events.php sql injection


📈 19.09 Punkte
🕵️ Sicherheitslücken

🕵️ Events Manager Plugin up to 5.8.1.1 on WordPress events-manager.js mapTitle cross site scripting


📈 19.09 Punkte
🕵️ Sicherheitslücken

🕵️ dotCMS 3.7.0 /news-events/events date cross site scripting


📈 19.09 Punkte
🕵️ Sicherheitslücken

🕵️ Low CVE-2013-7477: Wp-events-plugin Events manager


📈 19.09 Punkte
🕵️ Sicherheitslücken

🕵️ Low CVE-2013-7478: Wp-events-plugin Events manager


📈 19.09 Punkte
🕵️ Sicherheitslücken

🕵️ Low CVE-2013-7479: Wp-events-plugin Events manager


📈 19.09 Punkte
🕵️ Sicherheitslücken

🕵️ Low CVE-2013-7480: Wp-events-plugin Events manager


📈 19.09 Punkte
🕵️ Sicherheitslücken

🕵️ Content*Builder events/events.inc.php lang_path privilege escalation


📈 19.09 Punkte
🕵️ Sicherheitslücken

🕵️ Events Manager Plugin bis 5.8.1.1 auf WordPress events-manager.js mapTitle Cross Site Scripting


📈 19.09 Punkte
🕵️ Sicherheitslücken

🕵️ Events Manager Plugin bis 5.8.1.1 auf WordPress events-manager.js mapTitle Cross Site Scripting


📈 19.09 Punkte
🕵️ Sicherheitslücken

🔧 Understanding Domain Events in TypeScript: Making Events Work for You


📈 19.09 Punkte
🔧 Programmierung

🕵️ dotCMS 3.7.0 /news-events/events date Cross Site Scripting


📈 19.09 Punkte
🕵️ Sicherheitslücken

🔧 Testing Dependent Events in Flutter Bloc: Seeding State & Handling Multiple Events


📈 19.09 Punkte
🔧 Programmierung

🔧 -Events, Events Object, Window Elemnts, ClassList


📈 19.09 Punkte
🔧 Programmierung

🍏 Fix “Not authorized to send Apple events to System Events” Mac Error


📈 19.09 Punkte
🍏 iOS / Mac OS

🔧 Advanced Event Handling in JavaScript: Custom Events, Event Delegation, and Event Loop


📈 16.53 Punkte
🔧 Programmierung

🔧 C# Advanced: Unsubscribing from Events


📈 16.53 Punkte
🔧 Programmierung

🔧 C# Advanced:Events


📈 16.53 Punkte
🔧 Programmierung

🔧 Advanced Formulas Engagement, Top 19 YouTube SF Channels and Engagement Events with Data Cloud


📈 16.53 Punkte
🔧 Programmierung

🔧 Advanced Formulas Engagement, Top 19 YouTube SF Channels and Engagement Events with Data Cloud


📈 16.53 Punkte
🔧 Programmierung

🔧 Advanced C# Features: Exploring Delegates, Events, and Lambdas


📈 16.53 Punkte
🔧 Programmierung

🕵️ Medium CVE-2020-12104: Wp-advanced-search project Wp-advanced-search


📈 13.98 Punkte
🕵️ Sicherheitslücken

matomo