WPFMessageBoxService: replace at runtime

Jan 3, 2011 at 11:14 PM

I want to replace WPFMessageBoxService with a different implementation under certain circumstances.

I think this is possible with version 1 using Unity, but I cannot see if this is possible with version 2.
Any ideas?

<configuration>
  <configSections>
    <section name="unity"
        type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
              Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity>
    <containers>
      <container>
        <types>
          <type
              type="Cinch.IUIVisualizerService, Cinch"
              mapTo="Cinch.TestUIVisualizerService, Cinch"/>
          <type
              type="Cinch.IMessageBoxService, Cinch"
              mapTo="Cinch.TestMessageBoxService, Cinch"/>
          <type
              type="Cinch.IOpenFileService, Cinch"
              mapTo="Cinch.TestOpenFileService, Cinch"/>
          <type
              type="Cinch.ISaveFileService, Cinch"
              mapTo="Cinch.TestSaveFileService, Cinch"/>
        </types>
      </container>
    </containers>
  </unity>
</configuration>

 

Coordinator
Jan 4, 2011 at 8:16 AM

You may be better of asking Marlon about this over at his meffedMVVM site : http://mefedmvvm.codeplex.com/

I think it will either boil down to removing current WPFMessageBox service from MEF container and adding in new one, which may have to be in different assembly which is not part of current container, by writing custom MeffedMVVM IComposer. But best to ask marlon

Jul 8, 2013 at 1:54 PM
Hello sdemooy and Sacha,

I came across the same requirement today.
I tried to remove the Cinch's implementation from MEF's catalog, but with no success. The closer I got was to create a new IComposer (MEFedMVVM) and apply it to the bootstrapper:
MEFedMVVM.ViewModelLocator.LocatorBootstrapper.ApplyComposer(new MyRuntimeComposer());
Then I was able to remove Cinch's assembly from the catalog, but... all service implementations from Cinch were gone, not just the MessageBoxService I wanted to replace.

Then I ended up with the following solution.

Create the new IMessageBoxService implementation:
[PartCreationPolicy(CreationPolicy.Shared)]
[ExportService(ServiceType.Both, typeof(IMessageBoxService))]
public class MyMessageBoxService : IMessageBoxService
{
    //implementation details omitted
}
Create a new interface, like this:
public interface IMessageBoxServiceResolver
{
    IMessageBoxService Resolve();
}
The implementation uses the ImportMany parameter attribute to tell MEF to inject all avaiable implementations of IMessageBoxService from its catalog. The Resolve method is then in charge to choose one of these implementations:
[PartCreationPolicy(CreationPolicy.Shared)]
[ExportService(ServiceType.Both, typeof(IMessageBoxServiceResolver))]
public class MessageBoxServiceResolver : IMessageBoxServiceResolver
{
    private readonly IEnumerable<IMessageBoxService> services;
    private Type defaultServiceType = typeof(MyMessageBoxService);

    [ImportingConstructor]
    public MessageBoxServiceResolver([ImportMany]IEnumerable<IMessageBoxService> services)
    {
        this.services = services;
    }

    public IMessageBoxService Resolve()
    {
        return Resolve(defaultServiceType);
    }

    public IMessageBoxService Resolve(Type serviceType)
    {
        return services.FirstOrDefault(service => service.GetType() == serviceType);
    }
}
Finally, my ViewModel looks like this:
[ExportViewModel("SomeViewModel")]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class SomeViewModel : ViewModelBase
{
    protected readonly IMessageBoxService messageBoxService;

    [ImportingConstructor]
    public SomeViewModel(IMessageBoxServiceResolver messageBoxServiceResolver)
    {
        this.messageBoxService = messageBoxServiceResolver.Resolve();
    }
}
This implementation allows me to use MyMessageBoxService as the default implementation, while I still can use Cinch's implementation (by using the overloaded Resolve method).
I know this topic is from 2011 but... any suggestion is appreciated!
Best regards,