In MEF, from the System.ComponentModel.Composition assembly, it is possible to create a hierarchy of containers and use parent exports for child containers.
// ΡΠΎΠ΄ΠΈΡΠ΅Π»ΡΡΠΊΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΉΠ½Π΅Ρ var parentContainer = new CompositionContainer(new TypeCatalog(typeof(ParentService))); // Π΄ΠΎΡΠ΅ΡΠ½ΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΉΠ½Π΅Ρ var childContainer = new CompositionContainer(new TypeCatalog(typeof(Service)), parentContainer); // ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅ Π΄ΠΎΡΠ΅ΡΠ½ΠΈΡ
ΠΈ ΡΠΎΠ΄ΠΈΡΠ΅Π»ΡΡΠΊΠΈΡ
ΡΠΊΡΠΏΠΎΡΡΠΎΠ² ΡΠ΅ΡΠ΅Π· Π΄ΠΎΡΠ΅ΡΠ½ΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΉΠ½Π΅Ρ childContainer.GetExportedValue<IService>().DoWork(); childContainer.GetExportedValue<IParentService>().DoParentWork(); I am using MEF 2 from the Microsoft.Composition NuGet package. There was a need to create a hierarchy of containers. Is there the same opportunity here? I do the following:
var parentContainer = new ContainerConfiguration() .WithPart<ParentService>() .CreateContainer(); // ΠΠ°ΠΊ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΡΠ΄Π° ΡΠΎΠ΄ΠΈΡΠ΅Π»ΡΡΠΊΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΉΠ½Π΅Ρ? var childContainer = new ContainerConfiguration() .WithPart<Service>() .CreateContainer(); childContainer.GetExport<IService>().DoWork(); childContainer.GetExport<IParentService>().DoParentWork(); // ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅. ΠΠΊΡΠΏΠΎΡΡ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½