Question

How do I make sure that the container was either an instance of the class, or it was not, depending on whether there is a link to it or not. I using MEF container, C#

Edit: enter image description here

In other words, I need the ability to manage the lifetime of objects Shared classes. For example, I have two points (objects Obj1, Obj2), which import a reference to the Shared Object (Obj3), but these classes are NoneShared and do not live long, but the object (Obj3) - a link which they imported a large and expensive it is always keep in memory.

Maybe there are other IoC containers, which will provide more opportunities to work with the lifetime of objects?

Was it helpful?

Solution

Parts created with the Shared Creation policy will be held by the container and share the container's lifetime.

Even if the shared part is used to fill the imports on objects that have been disposed, the part will still exists with the container.

Here are the ways to get around that:

1: Change the Creation policy for the part to NonShared by using the following atribute on your export:

[PartCreationPolicy(CreationPolicy.NonShared)]

2: You can release a specific part by using the ReleaseExport method on the container (doc doc). Note that this will again only work with NonShared parts.

3: Use a child container for your shared part (doc). This way you can dispose of the child container, which will in turn dispose of it's shared parts. You can use the child container in a using to make sure it is disposed after use. This is the only way I see to get rid of a shared part in the container.

Oh and btw,

Maybe there are other IoC containers,

MEF isn't technically an IoC container. It uses the concept of IoC and you can use it like you would an IoC container but it's first and foremost and extension framework.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top