Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MEF import resolution

Let's say that I have a class with a set of fields marked with [Import] attributes where all these fields prohibit recomposition.

At some moment this class is instantiated using new MyClassWithInjectedFields(...) and all the corresponding dependencies are injected using the MEF framework - nothing serious, only bijective Export-To-Import mapping.

Questions:

  • Is it safe to assume that in this case the values of injected fields will remain the same until the class instance is garbage collected?

  • Or maybe there are some rare / obscure cases when they might get changed implicitly by MEF in runtime? Perhaps, when someone unloads or reloads the assembly used for dependency resolution?

(I am new to MEF and I'm not really sure if there is a way affect the dependency graph in runtime, but, well, there obviously might be).

like image 944
Yippie-Ki-Yay Avatar asked Oct 09 '12 11:10

Yippie-Ki-Yay


People also ask

Is MEF supported in .NET core?

For those who don't know, the Managed Extensibility Framework (MEF) is alive and well, and has been ported to . NET Core as System. Composition (source here).

What is MEF used for?

The Managed Extensibility Framework or MEF is a library for creating lightweight, and extensible applications. It allows application developers to discover and use extensions with no configuration required. It also lets extension developers easily encapsulate code and avoid fragile hard dependencies.

What is ImportingConstructor?

By using [ImportingConstructor] , you allow one class that serves as an export to import its dependencies. This dramatically simplifies the architecture, as you can decouple the dependencies of a concrete object from its implementation.

Is MEF a dependency Injection Framework?

MEF is a managed extensibility framework. It's a light-weight framework to build a plugin solution. At the base level, MEF will use reflection. In MEF the class or interface, we want to add it as a dependency.


1 Answers

1) MEF isn't doing anything special with the GC so the instances will not be GC'ed until there are no more live references to the objects.

2) If the Imports are not marked as recomposible then MEF will only ever change the values of Imports. You cannot unload\reload an assembly in a running .NET process, you can unload an AppDomain but in doing that you will essentially forcibly kill the object references but MEF doesn't do anything special to reset them.

like image 77
Wes Haggard Avatar answered Oct 02 '22 23:10

Wes Haggard