15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

754 ❘ ChaPTer 28 mAnAGed extensibility frAmewOrk<br />

Now you ’ ve seen imports, exports, <strong>and</strong> catalogs from the MEF architecture. Let ’ s get into the details <strong>and</strong><br />

different options of MEF by using a WPF application to host add - ins.<br />

ConTraCTs<br />

The following sample application extends the fi rst one. The hosting application is a WPF application that<br />

loads calculator add - ins for calculation functionality <strong>and</strong> other add - ins that bring their own user interface<br />

into the host.<br />

To get more information on writing WPF applications, you can read Chapter 35, “Core<br />

WPF,” <strong>and</strong> Chapter 36, “Business Applications with WPF.”<br />

For the calculation, the same contracts that were defi ned earlier are used: ICalculator <strong>and</strong> IOperation .<br />

Another contract is ICalculatorExtension . This interface defi nes the Title <strong>and</strong> Description properties<br />

that can be used by the hosting application. The method GetUI() returns a FrameworkElement that<br />

allows the add - in to return any WPF element that derives from FrameworkElement to be shown as the user<br />

interface within the host.<br />

using System.Windows;<br />

namespace Wrox.ProCSharp.MEF<br />

{<br />

public interface ICalculatorExtension<br />

{<br />

string Title { get; }<br />

string Description { get; }<br />

}<br />

}<br />

FrameworkElement GetUI();<br />

code snippet CalculatorContract/ICalculatorExtension.cs<br />

.<strong>NET</strong> interfaces make a good contract between the hosting application <strong>and</strong> the add - in. If the interface is<br />

defi ned in a separate assembly, as with the CalculatorContract assembly, the hosting application <strong>and</strong><br />

the add - in don ’ t have a direct dependency. Instead, the hosting application <strong>and</strong> the add - in just reference the<br />

contract assembly.<br />

From a MEF st<strong>and</strong>point, an interface contract is not required at all. The contract can be a simple string. To<br />

avoid confl icts with other contracts, the name of the string should contain a namespace name, for example,<br />

Wrox.ProCSharp.MEF.SampleContract , as shown in the following code snippet. Here, the class Foo is<br />

exported by using the Export attribute <strong>and</strong> a string passed to the attribute instead of the interface.<br />

[Export("Wrox.ProCSharp.MEF.SampleContract")]<br />

public class Foo<br />

{<br />

public string Bar()<br />

{<br />

return "Foo.Bar";<br />

}<br />

}<br />

The problem with using a contract as a string is that the methods, properties, <strong>and</strong> events provided by the<br />

type are not strongly defi ned. Either the caller needs a reference to the type Foo to use it, or .<strong>NET</strong> refl ection<br />

can be used to access its members. The <strong>C#</strong> 4 dynamic keyword makes refl ection easier to use <strong>and</strong> can be<br />

very helpful in such scenarios.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!