Question

I have been pondering on a question after i started studying Prism... why do we need to use a Service locator if we have MEF to satisfy all the imports required.

Common Service Locator link for MEF

I failed to understand what purpose it serves if the two coexists.

Does it allow Attributeless Imports (I believe exports have to be done using Exports) ??

I also read on this link on common service locator but didnt quite get the usage senario if bot if them co-exists.

Any help on this regads will be helpful... :)

Was it helpful?

Solution

There are dependency injection and service locator patterns. Both patterns can be used with MEF. When using [Import] or [ImportingConstructor], you're using dependency injection. On the other hand, when there' something like this in your code:

var someObject = exportProvider.GetExportedValue<ISomeType>();

you're using service location.

You can choose, which one is more suitable for you. Indeed, real applications often mixes these two patterns.

AFAIK, Prism uses service location at least for the bootstrapper, but Prism doesn't uses any DI-framework directly, because it wants to be independent from the particular DI-framework (there's MEF and Unity support by default). That's why Prism uses adapters for DI/Service location - they're just another level of abstraction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top