title: How can I Inject Auto Mapper directly in to Repository Layer, below the Controller
body: <p>This is an ASP.net Core 2.1 project.</p>
  
  <p>How can I Inject Auto Mapper directly into Repository Layer? Currently, I have the Auto Mapper being injected into the Controller and then from the controller, I pass it in the constructor to the Repo layer. </p>
  
  <p>It is possible to have it injected directly into Repo or is this the best that I can do?</p>
  
  <p>Here is my startup.ConfigureServices()</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      //** other stuff is here
  
  
  
      // Auto Mapper Configurations
      var mappingConfig = new MapperConfiguration(mc =&gt;
      {
          mc.AddProfile(new MappingProfile());
      });
  
      IMapper mapper = mappingConfig.CreateMapper();
      services.AddSingleton(mapper);
  
  }     
  </code></pre>
  
  <p>Here is a Controller</p>
  
  <pre><code>[Route("api/[controller]")]
  public class EmployeesController : Controller
  { 
      EmployeesRepo _repo;
  
      public EmployeesController(IMapper mapper)
      {
          _repo = new EmployeesRepo(mapper);
      }
  
      // GET: Jobs
      [HttpGet("[action]")]
      public IEnumerable&lt;EmployeeDTO&gt; GetAll()
      {
          //** TODO Fix Later hack for debug
          var employees = _repo.GetLast(100);
          return employees;
      }
  }
  </code></pre>
  
  <p>here is a Repo Class</p>
  
  <pre><code>public class EmployeesRepo
  {
      EFContext db = new EFContext();
  
      protected readonly IMapper _mapper;
  
      // Assign the object in the constructor for dependency injection
      public EmployeesRepo(IMapper mapper)
      {
          _mapper = mapper;
      }
  
      public IEnumerable&lt;EmployeeDTO&gt; GetAllEmployees()
      {
          return db.Employees.ProjectTo&lt;EmployeeDTO&gt;(_mapper.ConfigurationProvider).ToList();
      }
  
      internal IEnumerable&lt;EmployeeDTO&gt; GetLast(int v)
      {
         return db.Employees.ProjectTo&lt;EmployeeDTO&gt;(_mapper.ConfigurationProvider).ToList();
      }
  }
  </code></pre>