Messenger Instance Awareness

Sep 23, 2013 at 2:20 PM
I am using the Messaging portion of MVVM Light in a WinForm application. I am wondering what is the best method to distinguish between messages broadcasted to object instances of the same type. For example, the user is viewing several purchase orders for one customer. Each purchase order is a window of a particular type. If a message is sent that should apply to a only one of the open purchase order windows, what method do you use to have that message handled by that particular window.

I was thinking about different tokens, but that was kind of messy. Then I thought I could use Guid for the token passed in when the object was created. Also, I could create a type that included an instance guid. How do you handle this situation?

THanks.
Mike
Oct 15, 2013 at 10:10 AM
Edited Oct 15, 2013 at 3:07 PM
Your idea of the guid on object creation and a type that holds that guid and data you want to pass if probably your best bet, I use a different approach but it's the same idea. Since it's winforms you could consider using the window handle for unique identification.
public class ModuleHistory
    {
        public string ModuleName { get; set; }
        public string ModuleViewName { get; set; }
        public string EntityID { get; set; }
        public string EntityKindDesc { get; set; }
        public string EntityIndicator { get; set; }
        public DateTime? LoadTimestamp { get; set; }                                                                  
    }

 this.commandSelectEntity = new RelayCommand(() =>
            {
                _tempCount++;
                ModuleHistory mod = new ModuleHistory();
                mod.EntityID = MyModelEntity.ID.ToString();
                mod.EntityIndicator = ModuleInit.moduleIndicator; ;
                mod.ModuleName = ModuleInit.moduleName;
                mod.ModuleViewName = ModuleInit.moduleDefaultView;
                mod.EntityKindDesc = "Entity Description: " + MyModelEntity.ID.ToString();

                Messenger.Default.Send(new FrameworkMessage(new AppMessage
                {
                    MsgType = FrameworkMessageType.EntitySelected,
                    MsgValue = mod
                }));
            });

Listening module

Messenger.Default.Register<FrameworkMessage>(this, action => FrameworkCommand(action.Content));

 public void FrameworkCommand(AppMessage msg)
        {

            try
            {
                switch (msg.MsgType)
                {

                    case FrameworkMessageType.ModuleSearch:
                        {
My app is a Prism\WPF\MVVM app but you can use a similar approach perhaps. This approach works wells across modules too.
Oct 15, 2013 at 9:36 PM
Edited Oct 15, 2013 at 10:16 PM
mjhillman wrote:
I am using the Messaging portion of MVVM Light in a WinForm application. I am wondering what is the best method to distinguish between messages broadcasted to object instances of the same type. For example, the user is viewing several purchase orders for one customer. Each purchase order is a window of a particular type. If a message is sent that should apply to a only one of the open purchase order windows, what method do you use to have that message handled by that particular window.

I was thinking about different tokens, but that was kind of messy. Then I thought I could use Guid for the token passed in when the object was created. Also, I could create a type that included an instance guid. How do you handle this situation?

THanks.
Mike
I do the "GUID" token strategy when I have controls who have a view model that needs to send messages to another control with its specific view model. I do the "association" during the page/user control's OnLoad method. For example:
 private void OnLoaded(object sender, RoutedEventArgs e)
{
    var cumvm = (ComputerUserMasterViewModel)cuMaster.DataContext;
    var cudvm = (ComputerUserDetailsViewModel)cuDetails.DataContext;
    cumvm.Token = cudvm.GetHashCode();
}
So this way, I can have multiple instances of my Master control, and have it interact with a specific Details control, all by sending messages using the Token object as my messenger token.