Innovation. Imagination. Delivered. RSS 2.0
# Wednesday, July 22, 2009
Managed Extensibility Framework (MEF)

Below are my review notes from the 10-4 video on the upcoming Managed Extensibility Framework in version 4.0

  1. A new library available in v4.0
  2. Software entities should be open to extension, but closed to modification (Open/Closed Principle)
  3. Basics:
    • An application is built of parts or individual pieces composed together
    • Three Steps:
      1. Export a part (Attribute: [Export(typeof(IMortgageCalculator))])
      2. Import a part (Attribute: [Import(typeof(Logger))])
      3. Compose a part
        • Catalogs provide the parts (TypeCatalog, AssemblyCatalog, DirectoryCatalog, AggregatingCatalog)
        • Container is the matchmaker
  4. Why? - Build a network aware app without changing the core window code (i.e. extend behavior without modifying code)

Sample
namespace Sample
{
	public interface IMortgageRateRepository
	{
		public IList GetCurrentRates();
	}
}

Notice that in the concrete implementation of the interface we use the Export Attribute

namespace Sample
{
	[Export(typeof(IMortgageRateRepository))]
	public class ELoanMortgageRateRepository: IMortgageRateRepository
	{
		public IList GetCurrentRates()
		{
			return new List();
		}
	}
}

Alot of stuff happening here, we use the export attribute at the class level and then the Import attribute on our dependency. I also want to point out that we no longer 'new' up an object. Instead we implement the IPartImportsSatisfiedNotification interface and move out binding logic to that event handler (basically we can't use the objects until they have been imported by the container).

namespace Sample
{
	[Export]
	public partial class MortgageRates: Window, IPartImportsSatisfiedNotification
	{
		[Import]
		IMortgageRateRepository MortgageRateRepository  { get; set; }

		public MortgageRates()
		{
			InitializeComponent();

			//MortgageRateRepository = new MortgageRateRepository();
			//grid.ItemSource = MortgageRateRepository.GetCurrentRates();
		}

		public void OnImportsSatisified()
		{
			Dispatcher.Invoke(new Action(() =>
			{
				MortgageRateRepository = new MortgageRateRepository();
				grid.ItemSource = MortgageRateRepository.GetCurrentRates();	
			}));
		}
	}
}

Finally, where the magic happens. We create an AssemblyCatalog and then a CompositionContainer. Notice that instead of 'Newing' up an instance of the window directly. We get an instance of the exported object from the container.

namespace Sample
{
	public partial class App: Application
	{
		var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
		var container = new CompositionContainer(catalog);

		var window = container.GetExportedObject<MortgageRates>();
		//var window = new MortgageRates();

		window.Show();
	}
}
Wednesday, July 22, 2009 8:47:20 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] - Trackback
.Net v4.0 | Extensibility
Archive
<July 2009>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Christopher L Price
Sign In
Statistics
Total Posts: 13
This Year: 0
This Month: 0
This Week: 0
Comments: 0
Themes
Pick a theme:
All Content © 2010, Christopher L Price
DasBlog theme 'Business' created by Christoph De Baene (delarou)