如何在ASP.Net Boilerplate MVC项目中从Web Api调用应用程序服务?

问题描述 投票:0回答:1

ASP.NET Boilerplate (.Net Framework v4.7.2 & MVC)

我正在为一个应用服务类(AgentInfoAppService)创建一个web api控制器(AgentInfoController),它依赖于一个存储库IAAgentInfoRepository。在AgentInfoController中,我可以通过在构造函数中传递AgentInfoRepository来调用AgentInfoAppService中的方法,如下所示。new AgentInfoAppService(_agentInfoRepository).GetAgentCampaignswithCode 但我不确定这是否是正确的方法?

我们有一个从mvc层调用应用服务类(动态web api层)的工作代码,没有添加任何对Repository的引用,就像下面这样。有没有一种方法可以让我们添加一个依赖注入,这样我就不用在每次从web api层调用应用服务时添加仓库?

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);
    }
}
dependency-injection castle-windsor aspnetboilerplate asp.net-boilerplate
1个回答
0
投票

当你运行这个项目时 Abp 创建所有的方法,从 ApplicationService 层,你就可以使用它了。你不需要在控制器中再次创建方法。

下面是我说的一个例子。

我有一个 ApplicationServie 这样的方法

public class CiudadAppService : AsyncCrudAppService<AdozonaCiudad, CiudadDto, int, PagedAndSortedRequest, CiudadDto, CiudadDto>, ICiudadAppService
{

    private readonly ICiudadManager _ciudadManager;
    public CiudadAppService(IRepository<AdozonaCiudad> repository, ICiudadManager ciudadManager) : base(repository)
    {
        _ciudadManager = ciudadManager;
    }

    public override async Task<CiudadDto> CreateAsync(CiudadDto input)
    {

        var result = await _ciudadManager.RegistrarOActualizar(ObjectMapper.Map<AdozonaCiudad>(input));

        return MapToEntityDto(result);
    }
}

在此 AppService 我有一个方法叫 CreateAsync 这个方法可以在javascript中使用,就像这样。

(function ($) {

var _ciudadService = abp.services.app.ciudad;
var _$form = $('form[name=Index]');

function save() {

    _$form.validate({
        invalidHandler: function (form, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                var firstInvalidElement = $(validator.errorList[0].element);
                $('html,body').scrollTop(firstInvalidElement.offset().top - 100);
                firstInvalidElement.focus();
            }
        },

        rules: {
            Ciudad: "required"
        },
        messages: {
            Ciudad: "El valor del campo es requerido"
        },
        highlight: function (element) {
            $(element).parent().addClass('error-validation')
        },
        unhighlight: function (element) {
            $(element).parent().removeClass('error-validation')
        }
    });

    if (!_$form.valid()) {
        return;
    }

    var ciudad = _$form.serializeFormToObject(); //serializeFormToObject is defined in main.js

    abp.ui.setBusy(_$form);
    _ciudadService.create(ciudad).done(function () {
        location.reload(true); //reload page to see edited role!
        abp.notify.success('Registro correcto');
        $(".save-button").html('Guardar');
    }).always(function () {
        abp.ui.clearBusy(_$form);
    });
}
}

正如你所看到的,最重要的部分是,你正在创建一个服务,你可以访问你的所有方法。var _ciudadService = abp.services.app.ciudad; 有了这个,你就创建了一个服务,你就可以访问所有的方法。ApplicationService

希望能帮到你,如果你需要进一步的帮助请留言!

© www.soinside.com 2019 - 2024. All rights reserved.