简体   繁体   English

“绑定”在 DI 模式的上下文中是什么意思?

[英]What does the `binding` mean in the context of the DI pattern?

What does the binding mean in the context of the DI pattern? binding在 DI 模式的上下文中意味着什么?

I am going through the IoC tutorial over here .我正在这里浏览IoC教程。 And I come across the following excerpt:我遇到了以下摘录:

Dependency Injection (DI) is a design pattern used to implement IoC.依赖注入 (DI) 是一种用于实现 IoC 的设计模式。 It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways.它允许在类之外创建依赖对象,并通过不同的方式将这些对象提供给类。 Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.使用 DI,我们将依赖对象的创建和绑定移到依赖它们的类之外。

I can not understand what binding is referred to here?我不明白这里指的是什么绑定?

I guess that the binding referred to here is about setting the client class field.我猜这里提到的绑定是关于设置客户端类字段的。 But in such a case we are not moving such a binding with the DI.但在这种情况下,我们不会移动与 DI 的绑定。 Because, there are three ways to use the DI: constructor, property (setter) and method.因为,DI有三种使用方式:构造函数、属性(setter)和方法。 And all of those ways do not move the binding (the assignment of the client class field) outside of the class, all of them happen in the scope of the client class.并且所有这些方式都不会将绑定(客户端类字段的分配)移到类之外,所有这些都发生在客户端类的范围内。 So, I am confused here.所以,我在这里很困惑。

UPDATE After a few answers were provided to the question I come to a conclusion that there are two possible definitions of what a binding is.更新在对该问题提供了一些答案后,我得出的结论是,绑定是什么有两种可能的定义。

1 Binding means mapping an interface which is used inside the dependent classes to an actual object type. 1绑定意味着将依赖类内部使用的接口映射到实际的对象类型。

2 Binding means passing an actual type as an argument for an interface parameter into the dependent classes. 2绑定意味着将实际类型作为接口参数的参数传递到依赖类中。

What is the correct definition 1 or 2 ?正确的定义12 是什么? Or does the definition of the binding depend on the context where the binding is mentioned?还是绑定的定义取决于提到绑定的上下文?

What binding means?绑定是什么意思?

It means that our dependency classes will automatically be resolved by the IoC container.这意味着我们的依赖类会被 IoC 容器自动解析。

For example:例如:

We have IRepository interface and the Repository class .我们有IRepository interfaceRepository class

If we bind those two together.如果我们将这两个绑定在一起。 Every time we request the IRepository , our container automatically provides us with the Repository class.每次我们请求IRepository ,我们的容器都会自动为我们提供Repository类。

This makes it very easy to alter the implementation of the Repository class.这使得更改 Repository 类的实现变得非常容易。 Since we are not immediately dependent on it .既然我们不会立即依赖它 We never say new Repository class.我们从不说新的 Repository类。 We only provide the interface and everything else is taken care of by the container.我们只提供接口,其他一切都由容器来处理。 You could for example say the IRepository is bound to the DatabaseRepository class.例如,您可以说 IRepository 绑定到 DatabaseRepository 类。 With only changing your container bindings and nothing else.更改您的容器绑定而不更改其他任何内容。

This all happens due to the binding in the IoC container.这一切都是由于 IoC 容器中的绑定而发生的。 This provides a lot of flexibility inside your application.这在您的应用程序中提供了很大的灵活性。

Also what we can do with the binding command in our container can provide lifetimes for our objects usually those three(Singleton, PerInstance, Scoped).此外,我们可以使用容器中的绑定命令为我们的对象提供生命周期,通常是这三个(Singleton、PerInstance、Scoped)。

The class can be made unaware of the actual classes that get injected into it.可以使类不知道注入到其中的实际类。

It can rely on interface, instead of classes, where the logic of which class to instantiate in order to "fulfill" each interface can be delegated to code external to the classes themselves (or configuration).它可以依赖于接口,而不是类,其中实例化哪个类以“实现”每个接口的逻辑可以委托给类本身(或配置)外部的代码。

So we can say that the action of injecting a class where an interface is expected is like "binding".所以我们可以说在需要接口的地方注入一个类的动作就像“绑定”。

You can write code without Dependency injection like that:您可以编写没有依赖注入的代码,如下所示:

class NoDiRadio
{
    public EnergizerBattery Battery { get; set; }

    public NoDiRadio()
    {
        Battery = new EnergizerBattery(); // creation and binding inside of NoDiRadio
    }
}

class EnergizerBattery
{
    public void Start()
    {
    }        
}

However, you can write more loosely coupled code and more testable code like this:但是,您可以编写更松散耦合的代码和更可测试的代码,如下所示:

class DiRadio
{
    public IBattery Battery { get; set; }

    public DiRadio(IBattery battery)
    {
        Battery = battery;
    }
}

interface IBattery
{
    void Start();
}

class EnergizerBattery : IBattery
{
    public void Start()
    {
    }        
}

and then we move the creation and binding of the dependent objects outside of the class that depends on them .然后我们将依赖对象的创建和绑定移到依赖它们的类之外

IBattery battery = new DuracellBattery(); // creation and **binding** to IBattery here
var diRadio = new DiRadio(battery);

UPDATE:更新:

Binding in IoC-container means, when we see IBattery , then our IoC container will resolve an instance of DuracellBattery and inject it.在 IoC-container 中绑定意味着,当我们看到IBattery ,我们的 IoC 容器将解析一个DuracellBattery实例并注入它。 This is an example of Ninject Bind method:这是Ninject Bind方法的示例:

Bind<IBattery>().To<DuracellBattery>()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM