哪里"魔术"发生在Controller实例中的Web API DI正确接口的实现? [英] Where is the "magic" happening when a Controller instantiates the correct implementation of the Interface in Web API DI?

本文介绍了哪里"魔术"发生在Controller实例中的Web API DI正确接口的实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,在某个地方一只兔子被掏出帽子,当谈到DI在网页API控制器。

It seems to me that somewhere a rabbit is being pulled out of a hat when it comes to DI in Web API Controllers.

我神交是:
0)在Web API项目的控制器可与各种类被实例化,所有这些都实现在其上的控制器取决于该接口来调用。例如,用这种控制器code:

I grok that: 0) The Controller in a Web API project can be called with various classes to be instantiated, all of which implement the interface on which the Controller depends. e.g., with this Controller code:

private readonly IDepartmentRepository _deptsRepository;

public DepartmentsController(IDepartmentRepository deptsRepository)
{
    if (deptsRepository == null)
    {
        throw new ArgumentNullException("deptsRepository is null");
    }
    _deptsRepository = deptsRepository;
}

...deptsRepository可以是一个实现IDepartmentRepository并检索测试数据的类,或者它可以是一个实现IDepartmentRepository并检索生产数据,或一个类(等)

..."deptsRepository" can be a class that implements IDepartmentRepository and retrieves test data, OR it can be a class that implements IDepartmentRepository and retrieves production data, OR (etc.)

1)网页API决定哪些控制器基于URI的客户机调用被调用,并且该网页API决定是基于什么类型(GET,POST)等,这是什么指定参数时,如果该方法在控制器称为有的话,通过它。

1) Web API decides which Controller is called based on the URI the client calls, and that Web API decides which method in that Controller is called based on what type (GET, POST) etc. it is and what args, if any, are passed with it.

2),温莎城堡拦截此自动控制与自己更换路由引擎控制器。

2) Castle Windsor intercepts this automatic control of Controllers with its own replacement routing engine.

我不神交只是其中开发注入实现由控制器预期该接口的具体类。 IOW,如果我想运行,从测试数据拉班,我在哪里加code来指定?我认为它会在某个地方Global.asax.cs中,类似的信息(伪code):

What I don't grok is just where the developer injects the concrete class that implements the interface expected by the Controller. IOW, if I want to run the class that pulls from test data, where do I add code to specify that? I would think it would be somewhere in Global.asax.cs, something like (pseudocode):

// Use test data for now
DeptsControllerClass = TestDataClass;
//DeptsControllerClass = ProductionDataClass;

IOW,哪里之一指定,这一次,我希望你注入它实现所需的接口,该具体类?

IOW, where does one specify, "This time, I want you to inject THIS concrete class which implements the required interface"?

推荐答案

当你点1写道,路由和IOC是两回事。

As you wrote at point 1, Routing and IoC are two different things.

一旦路由引擎计算出该控制器已经被调用,控制工厂将被调用。

Once the routing engine figures out which controller has to be invoked, a "controller factory" will be invoked.

的WebAPI框架允许将您自己的工厂如下:

WebApi framework allows to plug your own factory as following:

GlobalConfiguration.Configuration.Services.Replace(
    typeof(IHttpControllerActivator),
    new WindsorCompositionRoot(this.container));

了解更多关于马克·西曼职位了解的WebAPI和放大器;温莎管道

Read more on Mark Seemann post about webapi&windsor plumbing.

哪个具体将用于满足给定的接口的依赖,这是由你选择如上的工厂内使用国际奥委会。使用温莎可能/应该链接interfce&放大器;混凝土在安装

Which concrete will be used to satisfy a given interface dependency, that's up to the ioc you choose to use within your factory as above. Using Windsor you may/should link interfce&concrete in the Installers.

让我试着回顾一下整个过程
1)设置路由,以VS控制器链接的URL
2)创建容器并注册使用安装所有组件
3)更换默认的控制器工厂W /工厂工作瓦特/你最喜欢的IoC容器(温莎,我presume :-))
4)享受的神奇。

Let me try to recap the whole process 1) Set up the routing in order to link url vs controller 2) create the container and register all components using installers 3) replace default controller factory w/ a factory working w/ your favorite ioc container(Windsor, I presume :-) ) 4) Enjoy the magic.

所有这些步骤happend在Global.asax

All those steps happend in the Application_start within the Global.asax

这篇关于哪里"魔术"发生在Controller实例中的Web API DI正确接口的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆