martavoi
martavoi

Reputation: 7092

CompositionInitializer missing from MEF 4.5. What can I use instead?

I can`t find System.ComponentModel.Composition.Initialization.dll in .NET 4.5 (Which contains the declaration of the CompositionInitializer class). Has this assembly been removed from MEF in .NET 4.5? How can I now compose parts of an application marked with [Export] and [Import] attributes?

Suppose I have this view:

    internal partial class StartWindow : Window
    {
        public StartWindow()
        {
            InitializeComponent();
            DataContext = ViewModel;
        }

        [Import]
        public IStartWindowViewModel ViewModel { get; set; }
    }

and the appropriate ViewModel:

    [Export]
    internal class StartWindowViewModel : IStartWindowViewModel
    {
        public IEnumerable<Customer> Customers { get; set; }
    }

What should I add in my shell (or elsewhere) to compose these parts?

Upvotes: 3

Views: 1459

Answers (2)

burning_LEGION
burning_LEGION

Reputation: 13450

you lost init composition, f.e.

    _container = new CompositionContainer(catalog);

    //Fill the imports of this object
    try
    {
        this._container.ComposeParts(this);
    }
    catch (CompositionException compositionException)
    {
        Console.WriteLine(compositionException.ToString());
    }

msdn

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564413

The CompositionInitializer and similar classes exist in Silverlight, but not the full .NET Framework. The MEF team purposefully decided to leave them out, as they felt that they were not appropriate.

The logic was that construction injection should be used instead.

That being said, the logic which applied in Silverlight for using the class applies exactly the same in WPF. I have blogged about this in .NET 4, and included an implementation that works for the full framework.

Upvotes: 5

Related Questions