0
我已經看到覆蓋了Caliburn.Micro引導程序,它可以方便地在Caliburn.Micro site上使用MEF。該倍率Caliburn.Micro的其他引導程序
public class MefBootstrapper : BootstrapperBase
{
private CompositionContainer container;
public MefBootstrapper() { Start(); }
protected override void Configure()
{
container = CompositionHost.Initialize(
new AggregateCatalog(
AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
)
);
var batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new WindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(container);
container.Compose(batch);
}
protected override object GetInstance(Type serviceType, string key)
{
string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
var exports = container.GetExportedValues<object>(contract);
if (exports.Any())
return exports.First();
throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
}
protected override void BuildUp(object instance)
{
container.SatisfyImportsOnce(instance);
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<IShell>();
}
}
現在,我看了一下Caliburn.Micros SimpleContainer
爲國際奧委會
public class AppBootstrapper : BootstrapperBase
{
SimpleContainer container;
public AppBootstrapper() { Start(); }
protected override void Configure()
{
container = new SimpleContainer();
container.Singleton<IWindowManager, WindowManager>();
container.Singleton<IEventAggregator, EventAggregator>();
container.PerRequest<IShell, ShellViewModel>();
}
protected override object GetInstance(Type service, string key)
{
var instance = container.GetInstance(service, key);
if (instance != null)
return instance;
throw new InvalidOperationException("Could not locate any instances.");
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
container.BuildUp(instance);
}
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
DisplayRootViewFor<IShell>();
}
}
我已閱讀MSDN docs on CompositionContainer,但我很困惑,如何建立引導程序配置都 IOC 和 MEF支持。這只是將上面兩個引導程序中的代碼組合起來的問題,如果是的話,我將如何從GetInstance
覆蓋返回?
謝謝你的時間。
MEF *是一個控制反轉的實現。我懷疑你們想要共存。 – Steve
但是MEF是一個插件模型嗎?它使[應用程序的可擴展性如此處所述](http://msdn.microsoft.com/en-us/library/dd460648(v = vs.110).aspx) – MoonKnight