What if a type is registered twice?

Topics: Prism v4 - Silverlight 4, Prism v4 - WPF 4
Mar 9, 2011 at 11:46 AM

Something I can't figure out from the documentation is what occurs if a certain interface is registered with two separate implementations? Which one is resolved?

E.g. We have the following types

Infrastructure.IService

ModuleA.ServiceA : Infrastructure.IService

ModuleB.ServiceB : Infrastructure.IService

ModuleC.ViewModelC tries to resolve Infrastructure.IService. Which concrete implementation will be returned? Or will this throw an exception?

Regards,

Neil

Developer
Mar 9, 2011 at 3:54 PM

Hi Neil,

Assuming you're using Unity, you can't register an interface mapping with two different implementations, unless you specify a name for the mappings. That way, you could have a name identifying the two separate implementations of the service, hence eliminating the ambiguity between them.

You might find this article from MSDN useful:

RegisterType Method

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Mar 9, 2011 at 4:06 PM
GuidoMaliandi wrote:

Hi Neil,

Assuming you're using Unity, you can't register an interface mapping with two different implementations, unless you specify a name for the mappings. That way, you could have a name identifying the two separate implementations of the service, hence eliminating the ambiguity between them.

You might find this article from MSDN useful:

RegisterType Method

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Thanks for that clarification, it makes sense. So this leads me on to another set of questions:

  1. What about if I'm using MEF? How are multiple mappings handled then?
  2. Since Prism is addressing loosely coupled UI frameworks and therefore module developers may not be part of the core project team, how can we control that only one implementation of a common interface is loaded in a set of modules that may have come from unknown sources? Named mapping can't address this issue since third-parties might not use a named mapping or use one that another third-party has also used.
  3. If we use the named mapping as you suggest, this would mean a decoupled module would need to ask for that implementation by name. That seems to go against the Prism way?

Neil

Developer
Mar 9, 2011 at 4:16 PM
Edited Mar 9, 2011 at 4:16 PM

Hi Neil,

In MEF you can specify a contract name for your export attribute, which would be similar to named mappings. You can read more about that in the Mef Programming Guide in Codeplex.

As for your second and third concerns, even though Prism is intended to address the difficulties of developing composite client applications, there are certain aspects of the development that should be agreed on even if there are separate teams working on a project, such as the name of the regions in which to add the views, or the name of the mapping in case it is known that there are going to be different implementations of the same interface. If you do need to avoid named mappings, you could try using child containers, but you should consider whether the overhead of using child containers is worth its benefits. You can read more about that in the following resources:

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi