Casey Wilkins
Casey Wilkins

Reputation: 2595

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 SendMessage(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?

Upvotes: 1

Views: 156

Answers (1)

BrandonZeider
BrandonZeider

Reputation: 8152

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.

Upvotes: 1

Related Questions