Chapter 9: Events and the publish-subscribe pattern
In this chapter
- 9.1 Event-driven programming
- 9.2 Why do we need events?
- 9.3 Delegate vs interface: how to choose?
- 9.4 Standard event pattern
- 9.5 Event design principles
- Principle 1: Use
EventHandler<T> - Principle 2: Keep event argument types consistent
- Principle 3: Provide an
OnEventNamemethod - Principle 4: Pass the event source as
sender
- Principle 1: Use
- 9.6 Events and memory leaks
- Solution 1: Remember to unsubscribe
- Solution 2: Use
WeakEventManager(WPF) - Anonymous disposal: limiting the scope of suspended notifications
- 9.7 Custom event accessors
- When do you need explicit event accessors?
- Thread-safe event implementation
- 9.8 Practical cases
- Event aggregator
- Summary
- Exercises
- Exercise 1: Implement the observer pattern
- Exercise 2: Implement an event aggregator
- Exercise 3: Event memory leak
- Glossary
This chapter is not available in the free preview. Purchase the book to read the complete edition now.