title: Validate AutoMapper configuration at startup
body: <p>We are using <code>Automapper</code> to map from the <code>Domain</code> object to <code>Repository</code> object and visa-versa. There is a deep nesting of child properties within a single domain. The <code>Repository</code> layer and Automapper have been added retrospectively. As a result, we have to create a Mapping <code>Profile</code> for several classes do the mapping.</p>
  
  <p>For example:</p>
  
  <h2>A sample <code>Domain</code> Class</h2>
  
  <pre><code>public class MyDomain 
  {
     public ChildDomain1 ChildDomain1  {get; set; }
  
     public IList&lt;ChildDomain2&gt; ChildDomain2  {get; set; }
  
     public ChildDomain3 ChildDomain3  {get; set; }
  
  .
  
  .
  
  .
  
  }
  </code></pre>
  
  <h2>A sample <code>Repository</code> Class</h2>
  
  <pre><code>public class MyRepository
  {
     public ChildRepository1 ChildRepository1  {get; set; }
  
     public IList&lt;ChildRepository2&gt; ChildRepository2  {get; set; }
  
     public ChildRepository3 ChildRepository3  {get; set; }
  
  .
  
  .
  
  .
  
  }
  </code></pre>
  
  <h2>Automapper <code>Profile</code> Mapping</h2>
  
  <pre><code>public class MapProfile : Profile
  {
      public MapProfile()
      {
          CreateMap&lt;MyDomain, MyRepository&gt;()
              .ReverseMap();
  
          CreateMap&lt;ChildDomain1, ChildRepository1&gt;()
              .ReverseMap();
  
          .   
  
          .
  
          .
     }
  }
  </code></pre>
  
  <h2>AutofacModel to register <code>Domain</code>/ <code>Repository</code> <code>Profile</code></h2>
  
  <pre><code>public class DocumentMapperAutofacModule : Module
  {
          protected override void Load(ContainerBuilder builder)
          {
              builder.RegisterAssemblyTypes(typeof(MapProfile).Assembly)
                  .Where(t =&gt; typeof(Profile).IsAssignableFrom(t) &amp;&amp; !t.IsAbstract &amp;&amp; t.IsPublic)
                  .As&lt;Profile&gt;();
  
              builder.Register(c =&gt; new MapperConfiguration(cfg =&gt;
              {
  
                  foreach (var profile in c.Resolve&lt;IEnumerable&lt;Profile&gt;&gt;())
                  {
                      cfg.AddProfile(profile);
                  }
              })).AsSelf().SingleInstance();
  
              builder.Register(c =&gt; c.Resolve&lt;MapperConfiguration&gt;()
                      .CreateMapper(c.Resolve))
                  .As&lt;IMapper&gt;()
                  .InstancePerLifetimeScope();
          }
   }
  </code></pre>
  
  <h2>Problem Statement</h2>
  
  <p>Currently, we get below Automapper exception, when a missing mapping is encountered while doing a map at the run time.</p>
  
  <blockquote>
    <p>AutoMapper.AutoMapperMappingException: Missing type map configuration
    or unsupported mapping.</p>
  </blockquote>
  
  <p>Our problem is that it is too late already.</p>
  
  <h2>Question</h2>
  
  <ul>
  <li><p>How do we ensure that a mapping exists for every child property in <code>Domain</code>/ <code>Repository</code> at the Application Start? Is there any easy way/ custom code to throw an exception at Application Start when mapping is missing?</p></li>
  <li><p>Or, is the unit/ integration tests to validate the mapping an only way around?</p></li>
  </ul>