ASP.NET Boilerplate (.Net Framework v4.7.2 & MVC)
I am creating a web api controller(AgentInfoController) for an application service class(AgentInfoAppService), which depends on a repository IAgentInfoRepository. From AgentInfoController, I am able to call a method from AgentInfoAppService by passing IAgentInfoRepository in the constructor like this new AgentInfoAppService(_agentInfoRepository).GetAgentCampaignswithCode
but I am not sure if this is the correct way?
We have a working code from mvc layer where we call application service class(dynamic web api layer) without adding any references to Repository like below. Is there a way we could add a dependency injection so I don't have to add the repository in each application service calls from web api layer?Please advise.
jQuery.ajax({
url: "/api/services/app/AgentInfoAppService/GetAgentCampaignswithCode?agentCode=100",
type: "GET",
contentType: "application/json; charset=utf-8"
})
public class AgentInfoAppService : ApplicationService, IAgentInfoAppService
{
private readonly IAgentInfoRepository _agentInfoRepository;
public AgentInfoAppService(IAgentInfoRepository agentInfoRepository)
{
_agentInfoRepository = agentInfoRepository;
}
public GetAgentCodeCampaignOutput GetAgentCampaignswithCode(string agentCode, bool? defaultCampaign)
{
var agentCampaigns = _agentInfoRepository.GetAgentCampaignswithCode(agentCode, defaultCampaign);
return new GetAgentCodeCampaignOutput()
{
AgentCodeCampaign = Mapper.Map<List<AgentCodeCampaignDto>>(agentCampaigns)
};
}
}
public class AgentInfoController : AbpApiController
{
private readonly IAgentInfoRepository _agentInfoRepository;
[HttpGet]
public GetAgentCodeCampaignOutput GetAgentCampaignswithCode(string agentCode, bool? defaultCampaign)
{
return new AgentInfoAppService(_agentInfoRepository).GetAgentCampaignswithCode(agentCode, defaultCampaign);
}
}