HELP MEF getting started.

Oct 24, 2012 at 4:35 PM

Hi I have been trying to get started with MEF 4.0

I have seen examples that suggest I should call

Container.Compose()

or

Container.ComposeParts(this)

but the composition container I get via:

var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); 
container = new CompositionContainer(catalog);

 does not have a method to ComposeParts or a Compose method with a default constructor.

Instead the Compose method I have access to wants a CompositionBatch object

container.Compose(batch)

...but I thought the CompositionBatch was to add manually constructed objects, and by preference we should always use the catalogue!

I am quite confused, it seems that MEF is a moving target and documentation I am finding relates to previous or future versions, it's difficult to get to grips from a beginner point of view.


Does anyone have a link to getting started with MEF that deals with the 'flavour' I have access too. "System.ComponentModel.composition 4.0.0.0"  That shows how I start-up a basic hello world WPF application?

Oct 24, 2012 at 4:44 PM

Hmm, so ComposeParts is an extension method!  Why?

Oct 24, 2012 at 5:29 PM

I am trying to create a WPF app with MEF: based on the "Meflook" example I now have in my App.xaml.cs this Compose method:

private bool Compose()
        {
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(Core).Assembly));

            _container = new CompositionContainer(catalog);

            //_container.AddPart(this);

            try
            {
                _container.ComposeParts();
            }
            catch (CompositionException CEx)
            {
                MessageBox.Show(CEx.Message);
                return false;
            }

            return true;
        }

 

_container does not have and AddPart Method,  how should I deal with this?

Oct 24, 2012 at 6:27 PM

So, this is the case where I use CompositionBatch?

So my compose method now looks like this.  Now that I am calling Compose(batch)  do I also need to call ComposeParts()?

        private bool Compose()
        {
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(TheCore).Assembly));

            _container = new CompositionContainer(catalog);


            try
            {
                var batch = new CompositionBatch();
                batch.AddPart(this);
                _container.Compose(batch);
            }
            catch (CompositionException CEx)
            {
                MessageBox.Show(CEx.Message);
                return false;
            }

            return true;
        }
Developer
Oct 25, 2012 at 4:42 PM

You can use the CompositionBatch directly if you like or you can use the CompareParts(...) extension method which does something similar to what you are doing manually here. To use the extension methods you need to include the "using System.ComponentModel.Composition;" namespace.

You want to use a batch or the extensions whenever you need to compose object instances that MEF itself did not create, in your case I'm assuming WPF created the object you are trying to compose.

 

Oct 27, 2012 at 2:03 PM
Edited Oct 27, 2012 at 2:07 PM

Thanks,

I am creating a small application to help me explore MEF,  it's 'Job' based app that reacts to files arriving in a folder, A job manager service I have is notified when a new file arrives, this jobmanger service then creates a new job to do stuff with the file, I cant think of a way to get MEF to magic me up a new job object, So I have the JobManager create the object manually.

The Job class wants a logger class ILoggerService (actually NLog)  but because I am manually creating the Job class the logger is obviously not composed. currently I am having the JobManager passes it's instance of the logger to each job it creates, but I get the feeling that there must be a better way.

If my JobManager had access to the container, can I call ComposeParts(Job)  i.e. is it ok to call composeParts many times in the app life cycle, or when I call composeParts(..) does that recompose the whole application,  is that what AllowRecomposition = false is for?