2014-02-25 43 views
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覆蓋返回?

謝謝你的時間。

+0

MEF *是一個控制反轉的實現。我懷疑你們想要共存。 – Steve

+0

但是MEF是一個插件模型嗎?它使[應用程序的可擴展性如此處所述](http://msdn.microsoft.com/en-us/library/dd460648(v = vs.110).aspx) – MoonKnight

回答

1

MAF可能是你在想什麼...它也被描述在那個MSDN鏈接。和之間的區別2.

正如史蒂夫指出,你可能不想同時擁有SimpleContainer和MEF,我相信IoC靜態類可能會有心臟病發作。實際上,我將使用MEF進行更大型的DI/IoC項目。它有時可能因爲它的力量而成爲幫助或負擔。 MEF所具有的侷限性之一就是開放泛型,它在嘗試開放泛型時死於可怕,據推測外部項目有助於緩解。

有幾個項目圍繞着與MEF集成MAF