Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Return IDisposible on Register (Maybe called RegisterScope??) in the IMessenger interface #70

Open
smeddows opened this issue May 6, 2019 · 0 comments

Comments

@smeddows
Copy link

smeddows commented May 6, 2019

I have a feature idea that I want to run by the community.

I have a situation where I'm in my application and I'm opening numerous windows (in the View Model (VM) using messages) and I need to listen to messages returned from the VM of the window that I'm opening. Here is a bit of the code that I'm using:

    Person chosenPerson = null;
        EmployerInfo EmployerInfo = null;
        bool? dialogResult = null;

        try
        {
            MessengerInstance.Register<FindPersonMessage>(this, message => chosenPerson = message.Content, true);
            
            MessengerInstance.Send(new OpenWindowMessage(caseType, result => dialogResult = result), MessageTokens.NewFindPersonFormChannel);

            MessengerInstance.Unregister<FindPersonMessage>(this);

            if (dialogResult.HasValue && dialogResult.Value && chosenPerson != null)
            {
                MessengerInstance.Register<SelectedEmployerMessage>(this, message => EmployerInfo = message.Content, true);

                MessengerInstance.Send(new OpenWindowMessage(chosenPerson, result => dialogResult = result), MessageTokens.NewFindEmployerFormChannel);

                MessengerInstance.Unregister<SelectedEmployerMessage>(this);

                if (dialogResult.HasValue && dialogResult.Value && EmployerInfo != null)
                {
                    MessengerInstance.Send(new ShowHideMessage(ShowHideMessage.GoToWindowState.Hide), MessageTokens.FindItemChannel);

                    MessengerInstance.Send(new OpenWindowMessage(chosenPerson, EmployerInfo));

                }
            }
        }
        finally
        {
            MessengerInstance.Unregister<FindPersonMessage>(this);
            MessengerInstance.Unregister<SelectedEmployerMessage>(this);

            MessengerInstance.Send(new ShowHideMessage(ShowHideMessage.GoToWindowState.Show), MessageTokens.FindItemChannel);
        }`

A command on the main form of my application will call this method on the VM which kicks off the workflow. Two form gather information by allow a user to search through some database/list and then finally hides the form that started the process and then calls the final form that will capture all the user's data.

I think this demonstrates where an IDisposible return would allow you to easily unregister instead of putting your UnRegister calls in your finally. It would still allow for strict adherence to using the Mediator pattern and allow for unit testing to still allow be modular and easy to implement the message handlers.

Maybe the method could even be called RegisterScope to denote that it's only valid for a particular scope in the code.

Any thoughts on this?

Thanks
Scott

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant