Getting Started with the Managed Extensibility Framework

The Managed Extensibility Framework (MEF) from Microsoft is a framework that allows developers to create a plug-in based application that allows for designing extensible programs by either the developer or third parties.  The definition from MSDN is as follows (link):

It allows application developers to discover and use extensions with no configuration required. It also lets extension developers easily encapsulate code and avoid fragile hard dependencies. MEF not only allows extensions to be reused within applications, but across applications as well.

At first glance, it looks like just another IoC container, and it certainly can be used for dependency injection much the same as Ninject or other DI frameworks. But the true power comes when you realize that your dependencies can come from anywhere and be loaded and run at any time, and that is the true purpose of MEF. It allows you to create libraries or pieces of functionality in isolation that perform a very specific functionality (as well as unit test them isolation as well), and then plug them in to a much larger application. This gives you very clean separation of concerns in your code, and allows developers to focus on smaller projects simultaneously and deliver a final product to the client that much faster.

[Read more…]

Best Practices for Dependency Injection

Dependency injection (DI) is a design pattern meant to transform hard-coded dependencies into swappable ones, generally at run-time. DI is the primary mechanism by which to implement Inversion of Control (IoC) techniques to load dependencies at run-time as well as the most effortless way to swap dependency implementations with mocks or stubs for unit testing. DI is a best practice that yields more readable and maintainable code due to the way all of an implementation’s dependencies are knowable at-a-glance and by the amazing side effect of creating easily testable code.
[Read more…]