开发者

MVVMLight : is this the right way to use the Messenger?

开发者 https://www.devze.com 2023-03-22 21:58 出处:网络
I have a classic business application that manages clients and adresses. There are tab items (Id, GenericInfo and a few more) with each their own ViewModel.

I have a classic business application that manages clients and adresses.

There are tab items (Id, GenericInfo and a few more) with each their own ViewModel.

There is a MainViewModel that handles the save and load commands of 开发者_如何学JAVAa client and its addresses.

We retrieve the data from a WCF service. The data received/sent from each WCF Function is aggregated in a different container.


In my MainViewModel I create a SaveContainer and then send it with the messenger.

    public void Save()
    {
        var container = new SaveContainer();

        MessengerInstance.Send(container);

        //the container is now populated and ready to be sent via WCF

        Console.WriteLine(container.User.Name);
        Console.WriteLine(container.Address.StreetName);
        Console.WriteLine(container.Address2.StreetName);
    }

In my UserViewModel is register for that container and then the viewmodel populate it with the data it has (the user).

    public UserViewModel()
        : base(Messenger.Default)
    {
        User = new User();

        MessengerInstance.Register<SaveContainer>(this, (x) => x.User = User);
    }

And in my AddressViewModel I do the same.

    public AddressViewModel()
        : base(Messenger.Default)
    {
        Address = new Address();
        Address2 = new Address() { StreetName = "Washington Street" };

        MessengerInstance.Register<SaveContainer>(this, x =>
        {
            x.Address = Address;
            x.Address2 = Address2;
        });
    }

I'd do the same when I have to load data.


After I send the Message, I assume that every ViewModel registered received the message and handled it. Am I assuming wrong? Do you find this way a correct way to use the Messenger? What would you improve?


There is no right way to use the messenger. However, you will have to consider that the message is handled by all recipients that have registerd for the message, not just an intended subset. Furthermore, when using messaging you do not have control over when the message handling is finished, now do you get notified when all recipients are done handling the message. In addition - depending on the implementation of the messenger - the messages may be handled in parallel.

So the problem with your approach (and @cadrell0's extension using a callback) is that you don't know when all recipients have handled the message. Using the callback you will get a callback for each recipient handling the message (i.e. n recipients n callbacks).

So how can you check when all recipients are done handling the message?

  • You use a counter to determine how many recipients have called back - this is error prone as you might register another message recipient and this messes up your system.
  • Another way would be validating the save container and once it is complete continue processing - but this might lead to a race condition as you may think all recipients have handled the message and continue, but then one late recipient calls in and invalidates your save container ... not good.

As I see it the messaging is more designed as a notification mechanism, i.e. you notify some recipients that something has happened. If you know and can ensure that there is only one recipient you even can use it in a manner you describe, but as soon as more than one recipient is involved this causes the mentioned problems.

So where does this leave you ... in your szenario I would tend to design the viewmodels as "related" (i.e. the main view model knows about the user view model and the address view models - or the main view model knows about the user view model that in turn knows about the address view models if that is more appropriate). Usually, I also would desing a model that holds the unit of work that I have to deal with (in your case the SaveContainer). Then all view models are constructed from this model and write their data to it. In normal cases this unit of work is what you get from your data storage service and what, in turn, gets saved by the data store in a single transaction.

But again, there is no right way to MVVM!


If I need to do something after a recipient responds to a message I include a callback on my message. When the recipient is done, it executes the callback. Adding parameters to the callback allows the recipient to send data to the sender. This also allows the recipient to perform an async operation.

0

精彩评论

暂无评论...
验证码 换一张
取 消