Skip to content

Instantly share code, notes, and snippets.

@dbuksbaum
Created August 21, 2010 19:43
Show Gist options
  • Save dbuksbaum/542762 to your computer and use it in GitHub Desktop.
Save dbuksbaum/542762 to your computer and use it in GitHub Desktop.
Caliburn.Micro BootStrapper that uses MEF
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