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

标签 dependency-injection castle-windsor aspnetboilerplate asp.net-boilerplate

ASP.NET 样板(.Net Framework v4.7.2 和 MVC)

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

我们有一个来自 mvc 层的工作代码,我们在其中调用应用程序服务类(动态 Web api 层),而不添加任何对存储库的引用,如下所示。有没有一种方法可以添加依赖项注入(inject),这样我就不必在来自 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);
    }
}

最佳答案

当您运行项目时,Abp会从ApplicationService层创建所有方法,您可以使用它。您不需要在 Controller 中再次创建该方法。

这是我所说的一个例子

我有一个像这样的 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

希望对您有帮助,如果您需要进一步帮助,请留言!

关于dependency-injection - 如何在 ASP.Net Boilerplate MVC 项目中从 Web Api 调用应用程序服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61513342/

相关文章:

javascript - Angular ui-router 中的 Promise 依赖解析顺序

android - 如果没有 @Provides 方法,则无法提供 Dagger 2 Named

c# - CaSTLe 依赖注入(inject) 生活方式

c# - 为什么要在 UpdateAsync 上设置 CreationTime 和 CreatorUserId?

javascript - 带有 SignalR 和简单 js 客户端的 Abp 通知

java - Dagger2 模块未注入(inject)其他模块或已弃用

java - 谷歌吉斯。使用@Provides注解注释的方法不会被调用

caSTLe-windsor - 温莎城堡 : how to pass arguments to deep dependencies?

caSTLe-windsor - MassTransit 2.6.1 请求/响应模式 - 响应超时

asp.net-core-2.0 - UseHangfireServer 抛出异常