user1792714
user1792714

Reputation: 129

Why use MEF for designtime/runtime in ViewModelLocator?

I have read a lot of articles in which MEF is used in the ViewModelLocator to separate designtime data and runtime data (to be specific, import designtiem ViewModels and runtime ViewModels). (I am talking about the VMLocator in the MVVM-light framework)

MEF is an excellent technology but isn't it designed for unknown dependencies? Obviously designtime / runtime ViewModels are known dependencies that should be handled by a pure IOC container.

Would someone please explain? Thanks

Upvotes: 3

Views: 473

Answers (1)

famousgarkin
famousgarkin

Reputation: 14116

Yes, MEF is primarily designed for unknown dependencies, but it sure can be used for the known ones as well. I've built an effective Service Locator on top of MEF myself just a few months ago.

I see a couple of reasons why it makes sense to go with MEF here:

  • Since .NET 4.0 it is an integral part of the framework. No need to have another 3rd party dependency.

  • It covers dependency discovery AND IoC. Does not matter if the API seems tailored towards discovery, you still get both features. And from my experience, dependency discovery is a great complement to IoC container.

  • It has good options for extensibility, which is often used just for these purposes, e.g. building your own parts catalog for view model locator to be able to supply design time models.

I think the reason why people choose MEF is that it provides all that is needed by these MVVM patterns and scenarios in one package and is available at hand. Not that it is necessarily the best in the field.

This is hardly an anwser you are looking for, but I have used MEF in a similar way, so I added an opinion :)

Upvotes: 2

Related Questions