Is dependency injection (DI) suitable for interfaces?

This may seem obvious to most people, but I'm just trying to confirm that Dependency Injection (DI) relies on the use of interfaces.

More specifically, in the case of a class that has a specific interface as a parameter in its constructor or a specific interface defined as a property (aka. Setter), the DI environment can pass an instance of a particular class to satisfy the needs of this Interface in this class. (Sorry if this description is not clear. I am having trouble describing this because the terminology / concepts are still somewhat new to me.)

The reason I'm asking is because I currently have a class that has a dependency. Not so much an object dependency as a URL. The class is as follows: [C #]:

using System.Web.Services.Protocols;
public partial class SomeLibraryService : SoapHttpClientProtocol 
{
        public SomeLibraryService() 
        {
            this.Url = "http://MyDomainName.com:8080/library-service/jse";
        }
}

The SoapHttpClientProtocol class has a Public property called Url(which is a plain old "string"), and the constructor here initializes it with a hard-coded value.

Can I use the DI framework to enter a different value when building? I do not think that since this.Urlit is not a type Interface; it is a String.

[, " wsdl", , . , , . , , .]

, , this.Url , , . (SoC),

?

+3
4

- . DI (, ) .

( DI, ), IoC , .

+6

DI , . , . , , .

, HttpContext.Current , , . DI- , HttpContext , .

+8

- . , , . "" # , Castle Windsor. (?) . , , DI , .

URL- , "" DI. Wikipedia DI vs framework DI.

+3

.NET-. .NET .

, .

"" ( ), , , , , ( "" ).

.NET , , .

, . , - , , .

, , DI , , , , .

IoC , , , , . "", , .

For me, the real strength of the IoC Framework is to switch implementations in places where you don't have control over the creation. For example, in ASP.NET MVC, creating a controller class is done using the ASP.NET structure, so injecting something is not possible. There are several hooks in the ASP.NET framework that the IoC framework can use to “interact between” the process of creating and executing their magic.

Luke

+1
source

Source: https://habr.com/ru/post/1754832/


All Articles