Bug 46601 - MessagingCenter is static
Summary: MessagingCenter is static
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms (show other bugs)
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-11-08 18:03 UTC by Josep Rodriguez
Modified: 2017-01-31 13:16 UTC (History)
6 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Josep Rodriguez 2016-11-08 18:03:21 UTC
# Issue

When using the Messaging center, there's no way to mock it as a dependency unless we write a wrapper layer around it.

In order to follow the SOLID principles and keep Xamarin Forms testable and easy to use:

* Define in an interface the methods we are currently using statically.
* Change the MessagingCenter class to have non static methods, implementing the interface created above.
* Create a singleton instance of the MessagingCenter that anyone can acquire so it keeps the same static behaviour the class had earlier.

If you follow this steps, developers using Xamarin Forms could inject into their classes an IMessagingCenter that could be mocked with Moq or similar tools, and the only change required in their code is replace:

MessagingCenter.Send/Subscribe/Unsubscribe...

with

MessagingCenter.SharedInstance.Send/Subscribe/Unsubscribe...
Comment 1 adrianknight89 2016-11-08 18:29:29 UTC
I like your suggestions.

Not sure if this is an option for you, but you could also try checking out the event aggregator in PRISM.
Comment 2 Josep Rodriguez 2016-11-08 18:35:26 UTC
Refering to this? https://msdn.microsoft.com/en-us/library/ff921122.aspx

I like the architecture proposed, but I feel like it will have a greater impact on consumers of the API.

I'd personally wouldn't mind to switch to it, since in the project I'm working on we are barely starting to use the MessagingCenter, but if it were in a later stage and it was more intensely used, I'd be a bit reticent to change it.
Comment 3 adrianknight89 2016-11-08 19:23:50 UTC
See https://github.com/xamarin/Xamarin.Forms/pull/514
Comment 4 Samantha Houts [MSFT] 2017-01-30 19:44:25 UTC
New PR: https://github.com/xamarin/Xamarin.Forms/pull/723
Comment 5 Rui Marinho 2017-01-31 13:16:36 UTC
Should be fixed in 2.3.5-pre1

Note You need to log in before you can comment on or make changes to this bug.