PartNotDiscoverable attribute in MEF

Managed Extensibility Framework as of now is a new model that deals with Extensibility in application by giving you a standard set of rules by which the vendor who exports components can be related easily to the vendors that imports software components. Hence MEF rules as a standard way of defining extensibility in application.

In MEF architecture we have a number of components that we need to address :

Import : Import is an attribute that marks an object of a particular type importable from 3rd party software vendors. Thus when you mark a Type as Import, it will indicate that the Type will get the object at runtime when Parts are composed.

Export : Export is an attribute that marks an object of a particular type exportable from a 3rd party software vendors. This will ensure that the object can be created at runtime and plugged in to some host application which is importing this component.

Compose : Compose is another important part of MEF architecture where the Imports and Exports are mapped together and relationship between them is created. Compose is done within a IOC container which writes the dependencies between components.
When you export some component the whole object is discovered at runtime when the Parts (a set of Import and Export) are created.  The CompositionContainer discovers all the Parts from a Catalog you specify within it and writes the dependencies between each component.  Now sometimes, you need to have some portion of your code not discoverable by the Catalog. PartNotDiscoverable is a special attribute that helps you to  deal with this situation.

[PartNotDiscoverable]
public Action MyActionDelegate { get; set; }

In this case the MyActionDelegate will not be discovered as a Part. The Catalog simply ignores this type as a Part and hence this portion of the code remains secure from external components.

It should be noted, PartNotDiscoverable attribute replaces the legacy [CompositionOptions(DiscoveryMode = DiscoveryMode.Never)] attribute.

Read more about it from the links :

Managed Extensibility Framework – A Look

Steps to write a plugin based application with MEF

Thank you for reading.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434

One Comment to “PartNotDiscoverable attribute in MEF”

Comments are closed.