Last active
December 31, 2015 22:29
-
-
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.
This file contains hidden or 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
/// <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