LightCZ
LightCZ

Reputation: 755

MVVM Light Messenger between Model -> ViewModel communication

I noticed that every child of ViewModelBase in MVVM Light framework can overwrite constructor

public ViewModelBase(IMessenger messenger)

Would it be (architectionaly speaking) ok to use IMessenger to Model to ViewModel communication and update ViewModels when Model change?

It would mean that every Model would have its own IMessenger instance and every viewModel would subscribe to those messages.

Now i am using interface such as this

public interface IModel
{
  event EventHandler<ModelStatusChangedArgs> StatusChanged;
}

and for each model which is kinda... pain and i feel i do something wrong.

Upvotes: 1

Views: 870

Answers (1)

Tseng
Tseng

Reputation: 64121

Technically you could use it for every situation where you don't know who may listen or be interested in that event.

But MVVM Light is an MVVM Framework, which is actually used to allow loose coupling of ViewModels. Using it inside your Model would ultimately tie your Models to your MVVM Framework.

But there is nothing that prevents you from writing your own Event Aggregator/Messaging system (i.e. Domain Events). You can also remove the Messanger dependency from your ViewModels by implementing a wrapper around MVVM Light's messenger.

You usually want keep this "layer" dependencies:

  • Your Infrastructure (ASP.NET, Persistence) should depend on your Domain/Business layer
  • Your Business Layer should not depend on any other Layers. This contains the core of your application and all of it's domain logic (business rules/logic)
  • Your Application Layer (ViewModels) shall depend only on your Domain/Business Layer. ViewModels only contain Presentation Logic
  • Your Presentation layer (Views) shall depend only on your ViewModels. Views should be unaware of Business Layer (Model in MVVM).

And the MVVM frameworks reside in the "Application Layer", Models shouldn't be aware of it.

Upvotes: 2

Related Questions