Add/remove dependencies dynamically

Apr 21, 2009 at 7:10 PM
Hi,

Is there a way to add/remove dependencies dynamically?

We're working in an application that builds itself dynamically, so would be nice if I could tell to a "part" what are gonna be its dependencies.

For example,  I import a Panel, and in the configuration I see I need put two CustomListBox, so I'd like tell to MEF "ey, look for CustomListBox in the catalog and bring it on".

I've been working with MEF only for a week, I've read the documentation but I feel like I don't really know how use all the power of MEF.

How may I do that?

Thanks in advance.
Developer
Apr 22, 2009 at 9:05 PM

No currently there is no way to dynamically add and remove imports on a given type. In fact our catalog system make assumptions that exports/imports are fixed for a given type so there will likely never be support for such a feature.

Traditionally if someone needs to get dependencies on the fly they would directly call container.GetExports(or GetExportedObject), however that should be avoided if at all possible. Part of the problem we are trying to solve with MEF is to allow people to statically declare their dependencies with the Import  attributes to allow for static analysis of dependency graphs and writing imperative code to retrieve dependencies circumvents this static analysis.

Apr 22, 2009 at 11:45 PM
Ok, what I'm trying to do,  is use MEF for find out which is the type I need (using the contract and the metadata) and after create new instances of that type. Can't I do that? How may I do it?

I'm trying now, but I always get the same instance, and in the case of visual elements ... I can't place the same element in two different ways.

Cheers.
Apr 23, 2009 at 8:21 PM
So, what happens if I use this extension method?

        public static T GetNew<T, W>(this Export<T, W> exp)
        {
            Type t = exp.GetExportedObject().GetType();
            return (T)t.Assembly.CreateInstance( t.ToString() );
            //return (T)Activator.CreateInstance( t );
        }

Something strange happens when that "new instance" has dependencies (I mean Imports), in the "OnImportsSatisfied" the  "imports" appears and are not null, but after that... they dissapear and the variables are null again ( I saw that in the Load event of one component).

What's going on? I may use MEF only as catalog and "inject" the dependencies by myself... but... is not the point :D

Cheers.
Apr 23, 2009 at 8:29 PM

It sounds like the first run through, in the debugger, you’re seeing the instance that is lazily created as a result of the GetExportedObject() call. This one will have its dependencies satisfied.

The second time you’re in the Load event, you’re seeing the instance created via the CreateInstance() call. Its dependencies will be null.

You can tell the instances apart by right-clicking ‘this’ in the Locals window and selecting Make Object Id.

Hope this helps,

Nick

From: Vtortolanet [mailto:notifications@codeplex.com]
Sent: Thursday, April 23, 2009 1:22 PM
To: Nicholas Blumhardt
Subject: Re: Add/remove dependencies dynamically [MEF:54034]

From: Vtortolanet

So, what happens if I use this extension method?

public static T GetNew<T, W>(this Export<T, W> exp)
{
Type t = exp.GetExportedObject().GetType();
return (T)t.Assembly.CreateInstance( t.ToString() );
//return (T)Activator.CreateInstance( t );
}

Something strange happens when that "new instance" has dependencies (I mean Imports), in the "OnImportsSatisfied" the "imports" appears and are not null, but after that... they dissapear and the variables are null again ( I saw that in the Load event of one component).

What's going on? I may use MEF only as catalog and "inject" the dependencies by myself... but... is not the point :D

Cheers.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com