托管扩展性框架(Managed Extensibility Framework)
【原文地址】
Managed Extensibility Framework 【原文发表日期】
28 April 08 06:14 Krzysztof 最近在他的blog上宣布,我们已经开始致力于为.NET创建一个扩展性的框架了……
在接下来的几个月,我们会在blog上谈论MEF的更多细节,不过现在已经有一些初期的细节了(当然,很可能会作改动):MEF是一系列特性的集合,在学术界与工业上,这一集合即是一个命名与激活服务(返回一个“具名”的对象),依赖注入(DI)框架,以及一个结构化类型系统(动态类型)。将这些技术(以及其它技术如System.AddIn)组织在一起,我们将实现一个称之为“开放式与动态应用”的新天地。比方说,它会使得创建可扩展的应用与扩展更加简单方便了。
[……]
最后,这里有一段代码,展示了我们的框架所支持的基本的场景:
在应用中创建一个扩展点:
public class HelloWorld {
[Import] // import declares what a component needs
public OutputDevice Output;
public void SayIt() {
Output.WriteLine("Hello World");
}
}
// Extension
public abstract class OutputDevice {
void WriteLine(string output){}
}
1. 创建一个扩展
[Export(typeof(OutputDevice))] // export declared what a component gives
public class CustomOutput : OutputDevice {
public void WriteLine(string output) {
Console.WriteLine(output);
}
}
2. 将应用于扩展结合(依赖注入)起来的诀窍
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();
我们乐于倾听您的意见……请加入到Kry的blog中一起讨论吧。
Read the complete post at http://blog.joycode.com/brada/archive/2008/05/19/115118.aspx