Quantcast
Channel: CleanCode NZ
Viewing all articles
Browse latest Browse all 28

Lifetime of parts created by MEF container

$
0
0

You might be confused that when lifetime of parts that are created by MEF container.GetExportedValues appeared to be NOT controlled by their container. the confusion comes from that fact that most of MEF documentations have not mentioned that parts could be referenced by other objects in your system apart from container, in that case, container will not be responsible for life time of parts SOLELY.

1. Parts created by container will be referenced by Container, if no other references are made to these parts, it will be freed when container is freed.

If other references are made to these parts apart from container, even container is freed, parts will live on as there are other references to these parts

2. If these parts are shared, [PartCreationPolicy(CreationPolicy.Shared)] or not any annotation (default to shared) , the instance only instantiated once even it is exported more than once by same container, note : same container, it only guarantees shared between one container. So if you have different container, it still can be instantiated twice.

I hope this helps you to understand the confusing explanations out there when you want to understand the lifetime of parts


Viewing all articles
Browse latest Browse all 28

Trending Articles