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

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

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:



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