Is Mef able to do that ?

Oct 29, 2010 at 10:18 AM

Hello,

I ve seen a lot of example showing that it's the instance with the imports which also call ComposeParts(this).

But is it possible to reverse the role : the class doing the export also wiring the parts:

 class Program
    {
        private CompositionContainer container;
        
        [Export("MessageContract")]
        static public string Toto = "Global Message";
        
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Run();
            Console.ReadLine();
        }

        private void Run()
        {
            var class1 = new Class1();
            class1.Display(); // -> Write Global Message to the ouput
        }

        public Program()
        {
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new DirectoryCatalog(@"."));
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));

            container = new CompositionContainer(catalog);

            try
            {
                //container.ComposeParts(this);
                //Call something to register the export for futures imports
            }
            catch (CompositionException compositionException)
            {
                Console.WriteLine(compositionException.ToString());
            }
        }
    }

// In a second assembly we found the following
public class Class1
    {
        [Import("MessageContract")]
        public string Message;

        public void Display()
        {
            Console.WriteLine("From Class1 " + this.Message);
        }
    }

The aim is to provide some global services (loggin, converters, translators,....) instanciated by the mainApp and consumed by the others classes without using cascades parameters in the constructors.
Regards,
Minskaya
Developer
Nov 7, 2010 at 7:47 AM

To do this you can either export the global services like you are doing for your "MessageContract". Or if you want to manually add them you can look at using a CompositionBatch with Container.Compose or the helper Container.ComposeExportedValue might be exactly what you want.

Nov 8, 2010 at 7:19 AM

Hi weshaggard and thanks for you help

weshaggard wrote:

To do this you can either export the global services like you are doing for your "MessageContract".

 The problem with what i do it 's that nothing is wired between the export and the imports : if you try my sample the Message is empty in the Display function.

My understanding is  that MEF is not able to wire instances created after the composition, am I right ? In this case may be Unity is more adequate ?

Regards,

Minskaya

 

Developer
Nov 8, 2010 at 4:10 PM

By default MEF will only set the Imports on an object instance that it creates, but in your case you are manually creating Class1. If you want to manually create object instances the only way to get the imports set is to have access to the container and call ComposeParts on the object instance.

        private void Run()
        {
            var class1 = new Class1();
            container.ComposeParts(class1);
            class1.Display(); // -> Write Global Message to the ouput
        }

Nov 9, 2010 at 7:16 AM

I understand.

Thanks a lot for the answer