What is the difference between CompositionBatch and directories?

I'm trying to wrap my brain around MEF, and one thing I'm struggling with is CompositionBatch .

CompositionBatch seems to be a way to add or remove exports to the MEF system, but apparently it cannot delete something added through the directory , therefore limited in usefulness. It also, for some reason, works at the container level, and not at the directory level with everything else that registers types in MEF.

As far as I can tell, CompositionBatch is just a redundant way to do what you can already do with directories. If I want to register two new types after starting my application, I do not need a CompositionBatch; I could just pass the AggregateCatalog to my container, and then when I am ready, create a TypeCatalog from the two new types that I want to register, add them to the AggregateCatalog, and everything will work. And yet there is a CompositionBatch that solves this problem with the directory by adding methods to the container instead.

What am I missing? Why is there a CompositionBatch? Does this really do what you cannot do with what was already there? Why do we need a way to do the same at the container level?

+4
source share
1 answer

If the type is in a directory, MEF will create an object of that type when necessary. CompositionBatch allows you to add objects to a composition that you create yourself. We recommend sticking with directories in general, but there are situations when you need to create an object yourself or it is created by some other component (for example, a XAML parser), and you need to put it in a container.

+6
source

Source: https://habr.com/ru/post/1346497/


All Articles