Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 402
Dzmitry Martavoi Avatar asked May 22 '26 03:05

Dzmitry Martavoi


2 Answers

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.

like image 172
Reed Copsey Avatar answered May 23 '26 15:05

Reed Copsey


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

like image 22
burning_LEGION Avatar answered May 23 '26 15:05

burning_LEGION



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!