- MEF Primitives Explained
-
Daniel just wrote a really nice post explaining the basics of MEF primitives. I recommend this to all interested in the internals or in extending MEF.
- Custom Programming Models for MEF (Provider Model Contrib)
-
The MEF composition engine operates on (composes) abstractions called ComposableParts. By default, parts are implemented as simple .NET classes annotated with MEF attributes (ExportAttribute and ImportAttribute). But, we envision that some parts will be implemented through variety of different mechanisms. For example, parts can be .NET types annotated with external files, DLR objects, XAML files, etc. We call such alternative means of specifying parts “custom programming models.”
In v1 of MEF, we focused on getting the default programming model super easy to use (most MEF users need to only understand a handful of types) and getting the ComposablePart abstractions right (making it possible to create custom programming models). We did not focus on making it easy to create custom programming models (quite advanced scenario).
Andreas stepped in and filled in that hole. He created a MEF contrib project which is a set of helper libraries that make it quite easy to create custom programming models. For all those who like to play around with internals of technologies like MEF, I recommend looking at the library.
- MEF Preview #4 Released
-
We have just released a new update to MEF. I am super excited about this release as it represents something quite close to what we are going to ship in terms of public APIs. In the last milestone, we have done quite significant API cleanup, renamed many core types to what I think will be their final names, and finally we have done some namespace factoring work. Now, the primitives, host APIs, and parts’ APIs are all in separate namespaces.
- PDC 2008 Talk: Framework Design Guidelines
-
Our PDC talk has been posted on Channel9. http://channel9.msdn.com/pdc2008/PC58/.
Here is the talk summary:
Learn about guidelines that have helped the Microsoft .NET Framework grow into the most popular developer framework Microsoft has ever created. After ten years of use, we have an enormous amount of real customer data about what makes great framework design. Whether you are building your own framework or just want to get the most out of the .NET Framework, this is a must-attend talk! Join Krzysztof Cwalina and Brad Abrams, authors of the Dr. Dobbs award winning "Framework Design Guidelines" book, and get a sneak peek at the content from the 2nd edition (first available at PDC2008).
- Framework Design Guidelines Videos
-
Brad and I just did a couple of video interviews that are now accessible online.
In the first one, we are talking about our PDC presentation (for those at the PDC, it’s at 4pm today). You can get it at 10 Years of Framework Design Guidelines (video).
The second interview is about just released 2nd edition of the Framework Design Guidelines book. You can get the video at Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries (video)
Enjoy!
- MEF on CodePlex
-
We have just released an update to MEF. You can get it at http://www.codeplex.com/MEF
The changes are quite significant:
1. The preview ships with sources under a very permissive license (Ms-LPL).
2. We now support constructor injection. Feature that the community asked for.
3. We completely redesigned MEF’s extensibility points. The extensibility points are designed to support writing custom providers of composition data. For example, out of the box MEF requires composable parts to be attributed with attributes that provide metadata describing the composition. We got lots of feedback that this is not acceptable in many scenarios. The new extensibility points make it easier to extend MEF to externalize the metadata (to an XML file for example). Note, that the changes are just the first step toward the goal of making the extensibility easy and powerful. We will most probably keep making improvements in this space in the future, so feedback on the new extensibility points would be more then welcome.
4. We significantly cleaned up the container APIs. But as above, there is more clean up to come in the future.
- Extensible Framework Design Studio Released
-
This summer we had a high school intern, Nick Moloney, who worked on incorporating MEF into FDS. The fruits of his labor are now on code gallery. You can download the extensible FDS here. Congratulations to Nick!
The current release has just a few extensibility points, but you should expect more in the future.
- API Design Myth: Exceptions are for "Exceptional Errors"
-
I was updating FDG section on exceptions. I added one anntation that I thought I would post here as well:
|
KRZYSZTOF CWALINA
One of the biggest misconceptions about exceptions is that they are for “exceptional conditions.” The reality is that they are for communicating error conditions. From a framework design perspective, there is no such thing as an “exceptional condition”. Whether a condition is exceptional or not depends on the context of usage, --- but reusable libraries rarely know how they will be used. For example, OutOfMemoryException might be exceptional for a simple data entry application; it’s not so exceptional for applications doing their own memory management (e.g. SQL server). In other words, one man’s exceptional condition is another man’s chronic condition. |
- Nullable<T> Usage Guidelines
-
These guidelines were just added as part of an update to the Framework Design Guidelines book (upcomming 2nd edition). Hope you find them useful.
Nullable<T> is a simple type added to the .NET Framework 2.0. The type was designed to be able to represent Value Types with “null” values.
Nullable<int> x = null;
Nullable<int> y = 5;
Console.WriteLine(x == null); // prints true
Console.WriteLine(y == null); // prints false
Note that C# provides special support for Nullable<T> in the form of language aliases for Nullable types, lifted operators, and the new coalescing operator.
int? x = null; //
long? d = x; // calls cast operator from Int32 to Int64
Console.WriteLine(d??10); // coalescing; prints 10 because d == null
þ CONSIDER using Nullable<T> to represent values that might not be present (i.e. optional values). For example, use it when returning a strongly typed record from a database with a property representing an optional table column.
ý Do NOT use Nullable<T> unless you would use a reference type in a similar manner, taking advantage of the fact that reference type values can be null.
For example, you would not use null to represent optional parameters.
// bad design
public class Foo {
public Foo(string name, int? id);
}
// good design
public class Foo {
public Foo(string name, int id);
public Foo(string name);
}
ý AVOID using Nullable<bool> to represent a general three-state value.
Nullable<bool> should only be used to represent truly optional Boolean values: true, false, and not available. If you simply want to represent three states (e.g. yes, no, cancel), consider using an enum.
ý AVOID using System.DBNull. Prefer Nullable<T> instead.
Nullable<T> is in general a better representation of optional database values. One thing to consider though it that while Nullable<T> gives you the ability to represent null values, you don’t get database null operational semantics. Specifically, you don’t get null propagation through operators and functions. If you deeply care about the propagation semantics, consider sticking with DBNull.
- POCO Support for MEF
-
Jason, our technical evangelist, just posted a sample showing how MEF can compose plain old CLR objects.
- MEF and System.AddIns
-
Several people asked about the relationship between MEF and the technology in System.AddIn namespace. The answer is that these two are independent and complementary features. MEF is a primarily a composition engine. System.AddIn is an add-in activation and isolation technology. MEF’s engine will be able to compose objects that are simple CLR object, COM and DCOM components (more precisely managed proxies to these components), remoting proxies, and finally System.AddIn add-ins.
The following is roughly how we see the basic “architecture” (conceptual diagram) of these technologies. As you can see on the diagram, the composition engine works on abstract representations of components currently called ComponentBinders. The composition engine is not concerned with how the parts are actually implemented or activated (COM, simple new operator, or System.AddIns activation). Its only concern is to inspect the binders for dependencies they need (imports) and objects they can give (exports), and provide “matchmaking” services for the binders (blue arrows on the diagram).

- MEF CTP 1 Released
-
Several members of my team have already spilled the beans, but yes (!) we just released our first public preview of MEF. You can grab the bits from here and read a past post for a high level overview.
I am super excited about the release. It’s a very early preview and still lots of work remains, but the CTP is a significant milestone for us. Designing a general purpose extensibility framework is hard, and so we felt very strong about releasing such an early preview to the world to get early feedback from the community. Hopefully together we can create a set of APIs that scale to the variety of extensibility scenarios: from developers using DI and IoC to large applications with extensibility points and plug-ins.
In my previous post about MEF, several people asked about constructor injection and non-attribute based programming model. Constructor injection is not supported in this preview, but we did start working on the design of the composition engine that would allow it. The non-attribute based programming model (where connections are specified externally) is actually enabled, but not built-in. I will try to post a sample showing how to do it, but for those that want to experiment themselves: you need to implement a custom component binder. The built-in binder inspects types and looks for the attributes. You custom binder might want to read the same information (which type to inject where) in some external file.
- Managed Extensibility Framework
-
Several months ago we formed what we call Application Framework Core team. The charter of the team is to play the same role in the application frameworks space (WinForms, ASP.NET, WPF, Silverlight) as the Base Class Libraries (BCL) team plays at the bottom of the platform stack.
The BCL team did a good job fulfilling the role of the team responsible for decreasing duplication and providing common abstractions for the low levels of the platform. Unfortunately, we did not have a similar team really focused on these sets of issues higher up on the stack. This resulted in some unfortunate duplication (like several data binding models for each of the application models, different dependency property system for WPF and WF) and lack of common abstractions (what undo APIs should my generic application plugin call?) for application model code. The Application Framework Core team is now in place to start addressing the problems.
One of the first concrete projects that we are working on and are ready to slowly talk about is what we call the Managed Extensibility Framework (MEF). We observed that there are more and more places in the .NET Framework itself and increasingly managed applications (like Visual Studio) where we want to provide, or already provide, hooks for 3rd party extensions. Think about TraceListener plugins for the TraceSource APIs, pluggable rules for Visual Studio Code Analysis (and the standalone FxCop), etc. In the absence of a built-in extensibility framework (like MEF), our developers who want to enable such extensions often are forced to create custom mechanisms, thus duplication. We hope that MEF will both stop such duplication and encourage/enable more extensibility in the Framework and applications built on top of it.
We will blog more details about MEF in the upcoming months, but here are some early details (subject to changes, of course): MEF is a set of features referred in the academic community and in the industry as a Naming and Activation Service (returns an object given a “name”), Dependency Injection (DI) framework, and a Structural Type System (duck typing). These technologies (and other like System.AddIn) together are intended to enable the world of what we call Open and Dynamic Applications, i.e. make it easier and cheaper to build extensible applications and extensions.
The work we are doing builds on several existing Microsoft technologies (like the Unity framework) and with feedback from the DI community. The relationship with the Unity team is the regular relationship between the P&P group and the .NET Framework group where we trickle successful technologies and ideas from the P&P team into the .NET Framework after they have passed the test of time. We have done this with some features in the diagnostics, exceptions, and UI space in the past. The direct engagement with the DI community is also starting. We gave a talk on the technology at last week’s MVP Summit, and talked with Jeremy Miller (the owner of Structure Map) and Ayende Rahien (Rhino Mocks) . We got lots of great feedback from Jeremy and Ayende and I think their experience in the DI space and their feedback will be invaluable as the project evolves. Thanks guys! We are of course also looking forward to engaging others in the DI community.
And finally here is some code showing basic scenarios our framework supports:
Creating an Extension Point in an Application:
public class HelloWorld {
[Import] // import declares what a component needs
public OutputDevice Output;
public void SayIt() {
Output.WriteLine("Hello World");
}
}
// Extension Contract
public abstract class OutputDevice {
void WriteLine(string output)
}
1. Creating an Extension
[Export(typeof(OutputDevice))] // export declared what a component gives
public class CustomOutput : OutputDevice {
public void WriteLine(string output) {
Console.WriteLine(output);
}
}
2. Magic that makes composes (DIs) the application with the extensions.
var domain = new ComponentDomain();
var hello = new HelloWorld();
// of course this can be implicit
domain.AddComponent(hello);
domain.AddComponent(new CustomOutput());
domain.Bind(); // bind matches the needs to gives
hello.SayIt();
Expecting lots of questions, I will preemptively answer (J): we don’t yet know whether or when we will ship this. We do have working code and we are looking into releasing a preview/CTP of the technology. For now we would be very interested in high level feedback. What do you think hinders extensibility in frameworks and application? Where would you like the Framework to be more extensible? What DI framework features you need, like, want, and use on daily basis? i.e. is constructor injection required?
And lastly, we are hiring! :-)
- Framework Design Guidelines Digest v2
-
Almost 4 years ago, I blogged about Framework Design Guidelines Digest. At that time, my blog engine did not support attaching files and I did not have a convenient online storage to put the document on, and so I asked people to email me if they want an offline copy.
Believe it or not, I still receive 1-2 emails a week with requests for the offline copy. Now that I have a convenient way to put the document online, and the fact that I wanted to make some small updates, I would like to repost the digest. The abstract is below and the full document can be downloaded here.
This document is a distillation and a simplification of the most basic guidelines described in detail in a book titled Framework Design Guidelines by Krzysztof Cwalina and Brad Abrams. Framework Design Guidelines were created in the early days of .NET Framework development. They started as a small set of naming and design conventions but have been enhanced, scrutinized, and refined to a point where they are generally considered the canonical way to design frameworks at Microsoft. They carry the experience and cumulative wisdom of thousands of developer hours over several versions of the .NET Framework.
- Framework Design Studio Released
-
When I was coming back from Mix 2007, I was bored on the plane and so started to write a dev tool. What a geeky thing to do on a plane. :-)
The tool allows comparing two versions of an assembly to identify API differences: API additions and removals. Comparing versions of APIs comes very handy during API design process. Often you want to ensure that things did not get removed accidentally (which can cause incompatibilities), and as APIs grow, you want to review the addition without having to re-review APIs that were already reviewed. The tool, called Framework Design Studio (FDS) supports these scenarios.
Later on, I got lots of help from Hongping Lim (a developer on our team), and David Fowler (our 2007 summer intern). David ported the application to WPF, and Hongping basically took it from an early prototype stage to what it is today and made it possible to ship it externally.
Anyway, you can get the tool at the MSDN Code Gallery, the user guide is attached to this post, and lastly, here is the API diff output that the tool generates. Hope you find it useful.
