Dynamic created objects?

Nov 17, 2008 at 8:59 AM
Thanks for MEF :)

I am doing this application (http://www.codeplex.com/WebMonitor), which is actually pretty old now, but I want to make it "Extensible" so that users of the application can easially write there own rules, that they want to run on the different monitors. Basically my application works like this:
- You set up an monitor (give it an URL to look at)
- At a given interval the monitor checks the URL and run different rules.
- The out come of the rule(s) is displayed to the user.

So, I want my rules to be "plugins".
You can see my implementation if you get changeset 17111 from the source at http://www.codeplex.com/WebMonitor. Note, it doesn't work yet, and I know I need to do some work at how the monitor workes with settings etc, but that is out of scope for this.

What am I trying to do?
In this testing phase I'm trying to get a list of rules (List<IMonitorRule>) via MEF.

So I have added this property to my Monitor:
public List<IMonitorRule> RuleList { get; set; } 

In the Ping() of the Monitor class I do a breakpoint and test is I get anything into this list:
public Monitor Ping()
            //Test: Here I want to check that MEF got the Imported list
            if (RuleList != null)
                int count = RuleList.Count;
            //More code here to ping the URL

When I get to this if, my RuleList is always = null, as the code is setup in the changeset 17111.

I think my problem is that my monitors is instantiated dynamicly, as the user creates them. This means that I don't actually put every instance of a monitor into my CompositionContainer.
I made testing code, where I added the actual instans of a monitor to the CompositionContainer everytime I added it to the collection, and that worked, but isn't there a way to make my scenario work without have to add all instances to the CompositionContainer?

I have looked at the MEF samples here from codeplex, and can't find a sample that fits...

Hope someone can help, thanks in advance.


Nov 18, 2008 at 12:33 AM
It sounds like you want to call container.SatisfyImports(monitor) for the monitors that are created outside of the container.  Let us know if you run into problems.

Nov 18, 2008 at 11:18 AM
Ok thanks I'll try that.
But i got the idea to take out this Rule list of each Monitor and put it in a more central place. Then I can pass in the RuleList to each monitor, instead of having it retrieved from MEF inside each Monitor.

I'll be back if I can't make it work :)

Nov 28, 2008 at 5:06 AM
Hi Daniel,

I've run into some problems getting SatisfyImports to work, are you able to assist.  I have a similar scenario to Qbus where SatisfyImports is just what I need however I can't get it to compile.

I have defined an Import in my class (lets say the Monitor class as above) that is created outside of the container and I am trying to call container.SatifyImports(monitor) as suggested, however I am getting a compile error because Monitor is not of type ComposablePart.

How can I get around this, am I doing something wrong?

Dec 2, 2008 at 11:01 PM
Hi tgc,

you'll have to create a ComposablePart from your Monitor object. The CompositionServices.CreateAttributedPart helper method will take in any object and return a ComposablePart (based on the presence of the MEF attributes on the type) which you can pass into SatisfyImports. You can discard the ComposablePart afterwards.

Hope that helps,