首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将IHttpClientFactory传递到类中

将IHttpClientFactory传递到类中
EN

Stack Overflow用户
提问于 2022-03-07 07:14:17
回答 1查看 45关注 0票数 0

如果我有一个控制器类,并且我想将它传递给处理我的HTTP调用的另一个名称空间,比如在下面的场景中,主任务调用TaskA() --它调用TaskG() --我需要像下面这样通过A将它传递给TaskG吗?或者是否有方法只在名称空间HttpClassFurtherDown中创建它,而不需要传递调用类。

代码语言:javascript
复制
namespace Controllers{
public class Drawings : ControllerBase
{
    private IHttpClientFactory _client {get;set;}
    public Drawings(IHttpClientFactory client)
    {
        _client=client;
    }

    [Route("api/Drawings")]
    [HttpPost]
    public async Task<ActionResult> PostAsync([FromBody] JsonFileContent[] content)
    {
        HttpExample e = new HttpExample(_client);
        e.TaskA();
        TaskB();
        return Accepted($"Drawings/Job/{id}");
    }
}}

namespace HttpClassExample{
public class HttpExample
{
  private IHttpClientFactory _client {get;set;}
    public HttpExample(IHttpClientFactory client)
    {
        _client=client;
    }
    public void TaskA()
    {
        DoSomeProcessing();
        HttpClassExampleFurtherDown e = new HttpClassExampleFurtherDown(client);
        e.TaskG();
    }
}

}
namespace HttpClassExampleFurtherDown{
public class HttpExampleFurtherDown
{
  private IHttpClientFactory _client {get;set;}
    public HttpExampleFurtherDown(IHttpClientFactory client)
    {
        _client=client;
    }
    public void TaskG(client)
    {
        //Finally Using It Here. I want to avoid having to generate it at the controller level and pass it all the way down.
        client.CreateClient();
        client.SendAsync();
    }
}

}
EN

回答 1

Stack Overflow用户

发布于 2022-03-07 10:10:21

,我想避免在控制器级别生成它,然后一直传递下去。

如果遵循DIP,那么在实际需要的地方注入显式依赖项,而不是与实现关注点紧密耦合。

虽然我认为提供的示例过于简化,但上面的示例应该是这样的

Controllers.Drawings

代码语言:javascript
复制
namespace Controllers{
    using HttpClassExample;
    //...

    public class Drawings : ControllerBase {
        private readonly IHttpExample client;
        public Drawings(IHttpExample client) {
            this.client = client;
        }

        [Route("api/Drawings")]
        [HttpPost]
        public async Task<ActionResult> PostAsync([FromBody] JsonFileContent[] content) {
            await client.TaskA();
            TaskB();
            return Accepted($"Drawings/Job/{id}");
        }
    }
}

HttpClassExample.HttpExample

代码语言:javascript
复制
namespace HttpClassExample{
    using HttpClassExampleFurtherDown;
    //...
    
    public class HttpExample : IHttpExample{
        private readonly IHttpExampleFurtherDown client;
        
        public HttpExample(IHttpExampleFurtherDown client) {
            this.client = client;
        }
        
        public async Task TaskA() {
            DoSomeProcessing();
            await client.TaskG();
        }
        
        //...
    }
}

HttpClassExampleFurtherDown.HttpExampleFurtherDown

代码语言:javascript
复制
namespace HttpClassExampleFurtherDown{
    public class HttpExampleFurtherDown : IHttpExampleFurtherDown {
        private readonly IHttpClientFactory factory;
        
        public HttpExampleFurtherDown(IHttpClientFactory factory) {
            this.factory = factory;
        }
        
        public async Task TaskG() {
            HttpClient client = factory.CreateClient();
            
            //...
            
            var response = await client.SendAsync();
            
            //...
        }
    }
}

这假设容器被用于管理基于其注册抽象的依赖实现的解析和注入。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71377552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档