Composed notification?

Mar 2, 2009 at 5:58 PM
  I have a class instance that contains an [Import]ed field.  This is very nicely being resolved for me during Compose().  However, I'd like to finish some initialization once this is available, but can't do it in the constructor, which is called before the field is set.
  I'm looking for (something like) an IComposed interface I can implement, which gets called once I'm Compose()ed.  Is this notification available and I'm just overlooking it?
Mar 2, 2009 at 8:07 PM

There is an interface (INotifyImportSatisfactoin) that you can use to get the behavior you’re after.

Often a better solution (in my opinion :)) is to use [ImportingConstructor] rather than [Import]. This will ensure that your objects can’t exist in a ‘semi-valid’ state. Whether this makes sense for you probably depends on how much work you’ll then need to do in your constructor.

(Not sure if you’ve encountered it, but the general rules here: http://docs.codehaus.org/display/PICO/Good+Citizen  work pretty well for component-based software as well. Take with requisite grain of salt.)

Nick

From: c0arndt [mailto:notifications@codeplex.com]
Sent: Monday, March 02, 2009 10:58 AM
To: Nicholas Blumhardt
Subject: Composed notification? [MEF:48964]

From: c0arndt

I have a class instance that contains an [Import]ed field. This is very nicely being resolved for me during Compose(). However, I'd like to finish some initialization once this is available, but can't do it in the constructor, which is called before the field is set.
I'm looking for (something like) an IComposed interface I can implement, which gets called once I'm Compose()ed. Is this notification available and I'm just overlooking it?

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

Mar 3, 2009 at 2:20 PM

Nicholas,
  Thanks for pointing out the interface, and for the codehaus reference.
  The instance that is Importing a list of 'widgets' is the host app instance itself.  I don't see any way to have MEF generate one for me.  MEF doesn't (or won't in the future) Import static's.  The only solution I can see is to have a temporary class that Imports the App, which would allow MEF to call the App ctor. 
  This must be a common situation.  Is it bad style to have the App doing any Imports?
--Chris

 

public class AppComposer {
[
Import]
public AppHost app;

 

 

static public AppComposer StartUpInstance()
AppComposer ac = new AppComposer();
    ac.Compose();
    return ac; }

 

 

private void Compose() {
  AssemblyCatalog catalog =
      new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
  var container = new CompositionContainer(catalog);
  var batch = new CompositionBatch();
  batch.AddPart(
this);
  container.Compose(batch);  }
} }

 

Mar 3, 2009 at 4:16 PM
Hi Chris,

Just realised your conundrum - yes, it is acceptable to use imports on the App class (check out the samples that ship with MEF) but I can see why importing into the App constructor is painful.

You might consider, in this one special case, adding an initialisation routine on the App class that you call directly yourself, e.g. if you use the structure that the MEF samples use, perhaps call YourInitialise() after calling Compose() on the container.

Hope this helps.
Nick
Mar 3, 2009 at 4:36 PM
Nick,
  In this case, I can do a lazy initialization the first time the calculated values are requested.

  I asked the original question because it seemed like an object might need to know once it was successfully Composed.

  I'd rather implement a Notifier than introduce an Initialize() method.

  Thanks again,

-- Chris