title: Automapper Configuration and Dependency Injection in ASP.NET Core for mutliple Apps
body: <p>Excuse me for this lengthy explanation, this to mention all details of my problem.</p>
  
  <p>I am using .NET Core and want to know where is the  best place I can put the AutoMapper Configuration? From where should I call it? And what is the best way to inject common services to several ASP.NET Core apps.</p>
  
  <p>Here is the situation right now, I have the following:</p>
  
  <ol>
  <li>Angular2 Application (client side)</li>
  <li>Resources API (ASP.NET Core APP for APIs)</li>
  <li>Authentication API (ASP.NET Core App for APIs)</li>
  <li>Service Layer (Class Library)</li>
  <li>Repository Layer (Class Library)</li>
  <li>Core (Class Library)</li>
  </ol>
  
  <p>In the repository layer, I have my Entity Framework classes (EntityModels Folder), and I have my implementation of Repositories where I use EF to get the data. The mapping is the happening at this level:</p>
  
  <pre><code>    public User Get(int id)
      {
          using (var securityDb = new SecurityContext())
          {
             var record = securityDb.Users.Where(u =&gt; u.UserId == id).SingleOrDefault();
  
             return AutoMapper.Mapper.Map&lt;User&gt;(record);
          }
      }
  </code></pre>
  
  <p>In the Core project, I have my domain and dto classes which are returned from Repository layer to the service layer.</p>
  
  <p>Both Services and Repositories project reference the Core Class library.</p>
  
  <p><a href="https://i.stack.imgur.com/5a7vN.png" rel="nofollow"><img src="https://i.stack.imgur.com/5a7vN.png" alt="enter image description here"></a></p>
  
  <p>Now, I should register all the dependencies, this is the "ConfigureServices" Startup Method in Authentication API Project where I am configuring AutoMapper by calling the Mapping class method.</p>
  
  <pre><code>services.AddTransient&lt;SecurityCore.RepositoryContracts.IUserRepository, SecurityCore.Repositories.UserRepository&gt;();
   services.AddTransient&lt;SecurityCore.ServiceContracts.IUserService, SecurityCore.Services.UserService&gt;();
  
  //Automapper configuration
    SecurityCore.Repositories.EntityModels.Mappings.Configure();
  </code></pre>
  
  <p>I have the mappings class in the repositories project as in the image above:</p>
  
  <pre><code>  public class Mappings
  {
      public static void Configure()
      {
          AutoMapper.Mapper.Initialize(cm =&gt; cm.CreateMap&lt;EntityModels.Users, Models.User&gt;());
      }
  }
  </code></pre>
  
  <p>This is some of my questions:</p>
  
  <ul>
  <li><p>Should I create a separate project that bootstrap all the mapping configuration, but this means that this project should reference both Core and Repositories project (instead of having the mapping config in Repositories)</p></li>
  <li><p>Should the automapping configuration be made at the ASP.NET Core
  as in my case?</p></li>
  <li><p>What about injecting dependencies code which is duplicated in both of ASP.NET Core projects, how I can separate the bootstrapping process? Should it be called once in every Application?</p></li>
  </ul>