title: Configuring Automapper in N-Layer application
body: <p>I have an N-Layer application as shown below</p>
  
  <p><strong>MyApp.Model</strong> - contains edmx and data models</p>
  
  <p><strong>MyApp.DataAccess</strong> - Repositories with EF</p>
  
  <p><strong>MyApp.Domain</strong> - Domain/business models</p>
  
  <p><strong>MyApp.Services</strong> - services(class library)</p>
  
  <p><strong>MyApp.Api</strong> - ASP.NET Web API</p>
  
  <p>I am using Unity as my IoC container and <strong>Automapper</strong> for OO mapping.</p>
  
  <p>My DataAccess layer references Model layer which contains all my Data objects.</p>
  
  <p>I do not want to refer my model project in my Api layer. So returning DomainObjects (business models) from service layer and mapping to DTOs in API layer(DTOs are in API layer).</p>
  
  <p>I configured domainModel to DTO mapping in API layer as below</p>
  
  <pre><code>public static class MapperConfig
  {
      public static void Configure() {
          Mapper.Initialize(
              config =&gt; {
                  config.CreateMap&lt;StateModel, StateDto&gt;();
                  config.CreateMap&lt;StateDto, StateModel&gt;();
  
                  //can't do this as I do not have reference for State which is in MyApp.Model
                  //config.CreateMap&lt;State, StateModel&gt;();
                  //config.CreateMap&lt;StateModel, State&gt;();
              });
      }
  }
  </code></pre>
  
  <p>Now my question is how/where to configure my auto mapper mappings to convert my Entity models to Domain models?</p>
  
  <p>To do in my API layer I do not have reference to my model project. I believe I should do this in service layer but not sure how to do that. Please help how to configure this mapping.</p>
  
  <p><strong>Note:</strong> Before asking here I googled with all eyes</p>
  
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/30136415/where-to-place-automapper-map-registration-in-referenced-dll">Where to place AutoMapper map registration in referenced dll</a>  says to use static constructor which I do not think a good option to add in all my models (I have 100 models) and another answer says to use PreApplicationStartMethod for which I have to add reference to System.web.dll to my services which is not correct.</p></li>
  <li><p><a href="https://groups.google.com/forum/#!topic/automapper-users/TNgj9VHGjwg" rel="noreferrer">https://groups.google.com/forum/#!topic/automapper-users/TNgj9VHGjwg</a> also did not answer my question properly.</p></li>
  </ol>