Skip to content

Instantly share code, notes, and snippets.

@martijnburgers
Last active December 31, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martijnburgers/8054222 to your computer and use it in GitHub Desktop.
Save martijnburgers/8054222 to your computer and use it in GitHub Desktop.
Our AutoMapper bootstrapper. For web applications you can call the Run method inside you application start or use something like WebActivator.
/// <summary>
/// Bootstraps AutoMapper.
/// </summary>
public static class DefaultAutoMapperBootstrapper
{
private static readonly object Lock = new object();
private static bool _initialized;
/// <summary>
/// Run the bootstrapper for AutoMapper
/// </summary>
public static void Run()
{
if (!_initialized)
{
lock (Lock)
{
if (!_initialized)
{
_initialized = true;
ConfigureAutoMapper();
}
}
}
}
private static void ConfigureAutoMapper()
{
var container = ObjectFactory.GetInstance<IContainer>();
if (container == null)
throw new InvalidOperationException("Where is our StructureMap container?");
var configuration = container.TryGetInstance<IConfiguration>();
if (configuration != null)
{
//saying AutoMapper how to resolve services - where are they located
configuration.ConstructServicesUsing(container.GetInstance);
foreach (var profile in container.GetAllInstances<Profile>())
{
configuration.AddProfile(profile);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment