MEF 2.0 – mini series: Part 7 (Catalog filter and Deep hierarchic scoping)
this is the 7th post in the MEF 2.0 mini series.
you can see the following TOC for other posts in this series.
in the previous post I was talking about composition scoping and lifetime management.
on this one, I will extend the composition scoping topic toward hierarchic along with catalog filtering capability.
hierarchic scoping is not trivial, you must understand the hierarchic behavior and what it was design for.
MEF hierarchic was design to enable parts from higher scope to access lower scope’s part without re-instantiation, lower part do not target higher scope’s part directly.
each hierarchic scope construct by importing a ExportFactory<T> (which we saw in the previous post).
I will try to make it as simple as I can.
the following diagram demonstrate the idea of sub scope that interact with its parent scope’s items.
let try to have a similar code structures.
I will use a plug-in base class to trace the instantiations:
derived class’s ids will be incremented per instantiation.
the following snippet include the classes which will be used for our hierarchic demonstration:
all the classes prefixed with the scoping ‘Level‘.
referring a sub-scope required using of ExportFactory<T> (line 23).
the following diagram is showing their hierarchic and dependencies graph:
our final step is to define a catalog per scope’s hierarchic and to set hierarchic structure.
at line 2-5, I export everything inherits from ‘PluginBase’ and adding a ‘ScopeLevel’ metadata with the right scope leveling value.
line 7, define a general assembly catalog (which will be the base for the filtered catalogs).
lines 9,10, are defining a filtered catalog for each scoping level (filtering an existing assembly catalog).
lines 12,13, defines the hierarchic between the scoped catalogs.
line 15, create a container with the top level scope.
now we’re ready for a composition.
I consider this feature as MEF 2‘s most complex one.
Hierarchic can become complex, so use it with caution.