locked
How can I get an exported value in MEF for metro style apps RRS feed

  • Question

  • I have an Interface like:

    public Interface MyInterface
    {
    }

    And a class implementing the interface:

    [Export(typeof(MyInterface))]
    public class MyClass : MyInterface
    {
    }

    In the normal MEF Version I can get the exported value from the container:

    MyInterface value = container.GetExportedValue<MyInterface>();

    How I can do this in Metro style apps? Im not asking for the SatisfyImportOnce method!

    Thanks & Best regards,

    Michael

    Thursday, March 8, 2012 8:18 PM

All replies

  • Hi,

    I am also interested in an answer to the question! Why has Microsoft removed the "CompositionContainer" class? In my opinion is MEF useless without this class.

    Bye,

    Sebastian

    Saturday, March 10, 2012 7:36 AM
  • Hi guys,

    I just wanted to drop you a note that we're looking at getting you an answer to this.

    -Steve

    Monday, March 12, 2012 6:17 PM
    Moderator
  • Hi Steve,

    thanks for reporting, I'm still waiting for a solution :)

    Michael

    Tuesday, March 13, 2012 11:54 AM
  • Hi Michael,

    Thanks for reporting this.  I am on the team that works on MEF and currently SatisfyImportsOnce is the only method we are exposing for composition.  This was done with the goal of simplifying composition.  Are there specifics of the scenario, that you can share, that are not supported by SatisfyImportsOnce ?

    Thanks

    -alok




    Tuesday, March 13, 2012 9:18 PM
  • Hi Alok,

    Yes there are some reasons.

    First of all, I consider property or field injection as a stablity risk. In my opinion, dependencies have to be injected by the constructor, where I can check them for null reference once and assign it to readonly variables. With SatisfyImportOnce I have to instantiate the object with dependencies manually, so I can not take the advantage of constructor injection.

    Second reason is the service locator pattern, where I retrieve specific exports from MEF. The servie locator does (should) not have access to the requesting instance and would have to create dinamically a type with the requested property to satisfy.

    I also have ohter issues concerning MEF for metro style apps. I want to register existing service instances to MEF, which was quite easy with the container:

    container.ComposeExportedValue<ILoggerFacade>(this.Logger);

    If you ask me, I would not simplify MEF a lot. Composition is an advanced technique so people knows what they are doing when they use it and I consider MEF already as easy to use. These limitations fro metro style apps made it more difficult for me.

    Thanks for asking and listening :)

    Michael

    Wednesday, March 14, 2012 9:44 AM
  • Hi Alok,

    for me the same things are important. Dependency injection via [ImportingConstructor] is the preferred method for me. And for this the composition container is essential!

    Can you explain why you have removed the center piece of MEF and left all other classes in the System.ComponentModel.Composition namespace?

    Bye,

    Sebastian

    Wednesday, March 14, 2012 7:35 PM
  • Hi, Ditto the above, I'm very interested to hear the outcome of this discussion. Cheers Jon
    Wednesday, March 14, 2012 9:35 PM
  • Michael, Sebastian, Jonathon,

       Thanks for your feedback and interest.  I will take this to the team ! We are striving the make MEF great, and your input is instrumental in guiding along. Will report back to the thread once I have some news.

    Cheers

    -alok

    Thursday, March 15, 2012 11:29 PM
  • Hi Alok,

    nice to hear that the microsoft team takes care our feedback and wishes :)

    Michael

    Friday, March 16, 2012 7:53 AM
  • @alokshriram

    Do you now have an answer?

    Monday, May 7, 2012 3:09 PM
  • Hi All, Sorry for the delay.

    http://blogs.msdn.com/b/bclteam/archive/2012/05/30/mef-and-tpl-dataflow-nuget-packages-for-net-framework-4-5-rc.aspx

    This is a posting to a version of MEF that should address your issues with MEF with Metro style apps.  Please give it a try and let us know what you think

    thanks

    -alok

    Thursday, May 31, 2012 10:13 PM
  • Thanks alok,

    I'm going to try it out, I will report you if it suits us or not.

    Michael

    Friday, June 1, 2012 3:15 PM
  • Deleted
    Tuesday, June 19, 2012 1:33 AM
  • Hi Bill, great to hear you've checked it out! The Microsoft.Composition package can be installed in both Metro style apps as well as regular .NET 4.5 apps, so you should be able to use it in both cases with the same code. Hope this helps! Nick
    Friday, June 22, 2012 8:52 PM
  • Hi everyone,

    To address the original question, it is possible to register instances in the Metro version of MEF (Microsoft.Composition).

    You need to use a small extension we provide as a sample in http://mef.codeplex.com/SourceControl/changeset/view/24db23e5045a#oob%2fdemo%2fMicrosoft.Composition.Demos.ExtendedPartTypes%2fProgram.cs

    This will enable syntax like:

        configuration.WithExport<IFoo>(foo);

    We will consider including this in the core in future, please send feedback on the extension if you have a chance to try it out.

    Thanks!

    Nick

    Friday, June 22, 2012 8:59 PM
  • Deleted
    Friday, June 22, 2012 11:33 PM