Created
August 21, 2010 19:43
-
-
Save dbuksbaum/542762 to your computer and use it in GitHub Desktop.
Caliburn.Micro BootStrapper that uses MEF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MefBootStrapper<TRootModelView> : Bootstrapper<TRootModelView> | |
{ | |
#region Fields | |
private readonly ILog _logger = LogManager.GetLog(typeof(MefBootStrapper<TRootModelView>)); | |
private CompositionContainer _container; | |
#endregion | |
#region Overrides | |
protected override void Configure() | |
{ // configure container | |
_container = new CompositionContainer( | |
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.Count() > 0) | |
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); | |
} | |
/// <summary> | |
/// The assemblies searched for a view | |
/// </summary> | |
/// <returns></returns> | |
protected override IEnumerable<Assembly> SelectAssemblies() | |
{ | |
return new[] { Assembly.GetExecutingAssembly() }; | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment