how to add scope if I use IService in Controller and connect Iservice in order as IService->Service->IRepository->Repository

144 Views Asked by At

This is the ApiController I want to use to get this Data.

[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
    private readonly IRegionService _service;
    public TestController(IRegionService service)
    {
        _service = service;
    }

    [HttpGet]
    public IEnumerable<RegionModel> Get()
    {
        return _service.GetAll();
    }

    [HttpGet, Route("api/region/getAll")]
    public IEnumerable<RegionModel> GetAll()
    {
        return _service.GetAll();
    }
}

Now To get this data, I go through the steps explained in the title.

IService->Service->IRepository->Repository

From Respository I get the data using LLBLGen. How do I put scope to use this repository in the project? I am also using AutoMapper and created Automapper.cs.

I added to the Automapper.cs as below

CreateMap<IRegionService, RegionService>();
CreateMap<IRegionRepository, RegionRepository>();

and getthing this error.

Unable to resolve service for type ' .Services.IRegionService' while attempting to activate ' .ApiController.TestController'.

1

There are 1 best solutions below

0
Farhad Zamani On

AutoMapper is a tool for Mapping, not for DI.

You must register IRegionService and IRegionRepository into DI in startup class like this

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IRegionService, RegionService>();
    services.AddScoped<IRegionRepository, RegionRepository>();
}