Bug 46601

Summary: MessagingCenter is static
Product: Forms Reporter: Josep Rodriguez <josep.rodriguez>
Component: FormsAssignee: Bugzilla <bugzilla>
Severity: normal CC: adrianknight89, ehart, jas, rui.marinho, sahou, stephane.delcroix
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

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