Take the following basic structured code for example:
class Player
{
public QuestLog QuestLog { get; set; }
}
Let us consider that "QuestLog" has the ability to fire off some events when things are added or removed from it. Normally, I would subscribe directly to the QuestLog, such as doing something like:
player.QuestLog.AcceptedQuest += SomeHandler
However, I want to pass in some extra context (the player object). I saw a couple options.
- Creating a lambda handler function which can resolve an extra parameter to the handler. This works but has some quirks I don't like. I'm currently using this approach.
- I could couple the QuestLog to a Player, passing a reference of the player it belongs to in and fire events off accordingly with it. Feels like needless coupling.
- Expose the event on the Player object itself as well as the QuestLog and 'bubble' the event up into where the context is available, thus allowing subscriptions directly onto player.
Keep in mind a player may have a few collections and a few events, so moving them all up might cause a couple dozen events to be held on one class.
How is this usually handled?