How should messages (not exceptions) be passed from the Model/Business Object layer to the UI?
Using VB.net 4.0
I have a Winforms application that is loosely based on MVVM. I'm looking for an easy way for any layer (even those that the UI has no reference to) to pass messages back to the UI for display to the user.
I have accomplished this in the past by creating a "Communicator" class in a "Common" assembly that every other assembly would reference.
Public Class Communicator
Public Shared Sub NotifyUser(Message as string)
RaiseEvent Se开发者_如何转开发ndMessage(Message)
End Sub
Public Shared Event SendMessage(MessageToSend as string)
End Class
The UI would subscribe to the SendMessage event at program startup. Any class wanting to pass a message to the user would simply call the Shared NotifyUser method and the Communicator class would relay the given message to the UI through the SendMessage event.
The upside to this method is that it is trivial to implement and super easy to use from anywhere in your code.
I suppose the downside to this is that calls to NotifyUser are spread throughout your code, making many classes dependent on the Communicator class and its shared method. For some reason, it just feels wrong.
So, my question is, what are some typical ways to achieve the same effect without a significant increase in complexity?
Honestly events are fine (with the downsides you pointed out). Another option is the Mediator pattern, which is more or less what it sounds like you've implemented, just without events.
精彩评论