【发布时间】:2014-02-25 12:25:48
【问题描述】:
我看到了 Caliburn.Micro 引导程序的覆盖,它有助于在 Caliburn.Micro site 上使用 MEF。覆盖是
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>();
}
}
现在,我已经阅读了有关 IOC 的 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 是控制反转的实现。我怀疑你是否希望两者共存。
-
但是MEF是一个插件模型不是吗?它支持应用程序的可扩展性,如here 所述
标签: c# inversion-of-control ioc-container caliburn.micro