Skip to content

Instantly share code, notes, and snippets.

@dbuksbaum
Created April 26, 2015 04:04
Show Gist options
  • Select an option

  • Save dbuksbaum/4aa176cc68bb1cd0e75d to your computer and use it in GitHub Desktop.

Select an option

Save dbuksbaum/4aa176cc68bb1cd0e75d to your computer and use it in GitHub Desktop.
Bootstrapping an Application with MEF and Autofac
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using Autofac;
using Autofac.Builder;
namespace BootstrapTest
{
public sealed class Bootstrap
{
#region Static Fields
private static readonly object _lockObject = new object();
private static Bootstrap _instance;
#endregion
#region Properties
[ImportMany(typeof(IModule))]
internal IModule[] Modules { get; set; }
#endregion
#region Constructors
private Bootstrap()
{
}
#endregion
public static IContainer Initialize(CompositionContainer container)
{
return Initialize(container, false);
}
public static IContainer Initialize(CompositionContainer container, bool resetSingleton)
{
Bootstrap bootStrap;
lock (_lockObject)
{ // only work on bootstrap instance in one thread at a time
if (resetSingleton || _instance == null)
{ // create and MEF initialize the bootstrap object
bootStrap = new Bootstrap();
container.SatisfyImportsOnce(bootStrap);
// store in singleton
_instance = bootStrap;
}
// use current instance
bootStrap = _instance;
}
// now initialize autofac
ContainerBuilder builder = new ContainerBuilder();
// add support for implicit collections
builder.RegisterModule(new Autofac.Modules.ImplicitCollectionSupportModule());
// add all MEF-ed modules
foreach (Module module in bootStrap.Modules)
builder.RegisterModule(module);
// create the container
return builder.Build();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Reflection;
namespace BootstrapTest
{
public sealed class CatalogConfigurator
{
#region Fields
private readonly object _lockObject = new object();
private readonly AggregateCatalog _currentAggregateCatalog = new AggregateCatalog();
private CompositionContainer _container;
#endregion
public CatalogConfigurator AddCatalogs(params ComposablePartCatalog[] catalogs)
{
foreach (var catalog in catalogs)
_currentAggregateCatalog.Catalogs.Add(catalog);
return this;
}
public CatalogConfigurator AddCatalogs(IEnumerable<ComposablePartCatalog> catalogs)
{
if (catalogs != null)
{
foreach (var catalog in catalogs)
_currentAggregateCatalog.Catalogs.Add(catalog);
}
return this;
}
public CatalogConfigurator AddAssembly(Assembly assembly)
{
return AddCatalogs(new AssemblyCatalog(assembly));
}
public CatalogConfigurator AddAssembly(string codeBase)
{
return AddCatalogs(new AssemblyCatalog(codeBase));
}
public CatalogConfigurator AddDirectory(string path)
{
return AddCatalogs(new DirectoryCatalog(path));
}
public CatalogConfigurator AddDirectory(string path, string searchPattern)
{
return AddCatalogs(new DirectoryCatalog(path, searchPattern));
}
public CatalogConfigurator AddTypes(params Type[] types)
{
return AddCatalogs(new TypeCatalog(types));
}
public CatalogConfigurator AddTypes(IEnumerable<Type> types)
{
return AddCatalogs(new TypeCatalog(types));
}
public CompositionContainer BuildContainer()
{
return BuildContainer(false);
}
public CompositionContainer BuildContainer(bool rebuildContainer)
{
lock (_lockObject)
{
if (rebuildContainer || (_container == null))
_container = new CompositionContainer(_currentAggregateCatalog);
}
return _container;
}
}
}
namespace SampleInterface
{
public interface IStuff
{
void DoSomething();
string Echo(string text);
}
}
using System.ComponentModel.Composition;
using Autofac;
using SampleInterface;
namespace ModuleTestA
{
[Export(typeof(IModule))]
public class MyModuleA : Autofac.Builder.Module
{
protected override void Load(Autofac.Builder.ContainerBuilder builder)
{
builder.Register<StuffA>().As<IStuff>();
}
}
}
using System;
using System.Reflection;
using Autofac;
using SampleInterface;
namespace BootstrapTest
{
class Program
{
static void Main(string[] args)
{ // configure and create MEF container
var mefContainer = new CatalogConfigurator()
.AddAssembly(Assembly.GetExecutingAssembly())
.AddDirectory("Extensions")
.BuildContainer();
// now configure and create autofac container
IContainer container = Bootstrap.Initialize(mefContainer);
container.Resolve<IStuff>().DoSomething();
Console.WriteLine("Press ENTER to quit.");
Console.ReadLine();
}
}
}
using System;
using SampleInterface;
namespace ModuleTestA
{
public class StuffA : IStuff
{
#region IStuff Members
public void DoSomething()
{
Console.WriteLine(“StuffA is Doing Something!”);
}
public string Echo(string text)
{
return text;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment