【问题标题】:Autofac SingleInstance WebApi PerformanceAutofac SingleInstance WebApi 性能
【发布时间】:2018-05-22 15:42:12
【问题描述】:

我们改进了 API 向 .SingleInstance() 添加无状态服务的性能,但我有一个问题,关于附加的演示代码,

  • 我们在控制器上使用的 IBusAppService 设置为 SingleInstance(),但在 BusAppService 内部,我们使用了更多接口,例如(IBusRepository 或 IBusDomainService)
  • 那么问题来了,为了提高性能,我们应该将所有接口都设置为 IBusAppService 内部的 SingleInstance() 还是因为它们在 SingleInstance 内部而性能相同??

我将在此处附上工作流程的一些代码:

ApiController:

    public class BusApiController : ApiController
    {
        private readonly IBusAppService _iBusAppService;
        private readonly IBusMapper _iBusMapper;
        public BusApiController(IBusAppService iBusAppService,
                                IBusMapper iBusMapper)
        {
            _iBusAppService = iBusAppService;
            _iBusMapper = iBusMapper;
        }

        [HttpGet]
        public BusResponse Get(long id)
        {
            var bus = _iBusAppService.Get(id);

            var busResponse = _iBusMapper.Convert(bus);
            return busResponse;
        }
    }


    public class BusResponse {
       public long Id { get; set; }
    }

    public interface IBusMapper
    {
        BusResponse Convert(Bus bus);
    }

    public class BusMapper : IBusMapper
    {
        public BusResponse Convert(Bus bus)
        {
            if (bus == null) return null;

            var result = new BusResponse{Id = bus.Id};
            return result;
        }
    }

    builder.RegisterType<BusAppService>().As<IBusAppService>().SingleInstance();
    builder.RegisterType<BusMapper>().As<IBusMapper>().SingleInstance(); 

应用程序服务

public interface IBusAppService
{
    Bus Get(long id);
}

public class BusAppService : IBusAppService
{
    private readonly IBusRepository _iBusRepository;
    private readonly IBusDomainService _iBusDomainService;
    public BusAppService(IBusRepository iBusRepository, IBusDomainService iBusDomainService )
    {
        _iBusRepository = iBusRepository;
        _iBusDomainService = iBusDomainService;
    }
    public Bus Get(long id)
    {
        var bus = this._iBusRepository.Get(id);
        var busTax = this._iBusDomainService.CalculateTax(bus);

        var result = bus;
        return result;
    }
}

【问题讨论】:

    标签: c# asp.net-web-api2 autofac single-instance


    【解决方案1】:

    由于captive dependencies,单实例服务消耗的任何东西最终都将成为单实例。您也可以将它们更改为单实例,但这不一定会改变您现在看到的与实例化成本相关的性能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      相关资源
      最近更新 更多