Why don't languages ​​integrate Injection Dependency into the kernel?

Why don't languages ​​integrate Injection Dependency into their core (at the lowest level: compilation can be), since dependencies are the root of all evil in complexity theory? This will avoid using the Framework.

I rather aim at compiled languages.

My question is similar to duck typing, which was recently introduced with dynamics in .NET. Why not something similar in the future for DI?

Update: hey guys, this item seems to be of interest, any voters for reopening it :)

+8
java c # dependency-injection frameworks inversion-of-control
source share
6 answers

I personally see this as a benefit. For example, I write this code all the time:

public class MyService : IService { // Boilerplate! private readonly IDependency1 dep1; private readonly IDependency2 dep2; private readonly IDependency3 dep3; public MyService(IDependency1 dep1, IDependency2 dep2, IDependency3 dep3) { // More boilerplate! this.dep1 = dep1; this.dep2 = dep2; this.dep3 = dep3; } // Finally something useful void IService.DoOperation() { using (var context = this.dep1.CreateContext()) { this.dep2.Execute(context); context.Commit(); } this.dep3.Log("Success"); } } 

It would be nice to write this as follows:

 public class MyService : IService { public MyService(private IDependency1 dep1, private IDependency2 dep2, private IDependency3 dep3) { } void IService.DoOperation() { using (var context = this.dep1.CreateContext()) { this.dep2.Execute(context); context.Commit(); } this.dep3.Log("Success"); } } 

I'm sorry that I can’t just leave all the plumbing with the declaration of the dependency fields and assigning them in my constructor.

UPDATE

Our prayers may have been answered. The C # team can add “concise syntax for class definitions,” such as properties that “can be declared directly from the constructor” in C # 6.0. Let hope such a feature makes light.

So your main question is: “Why don't languages ​​integrate Injection Dependency into the kernel?” They do. Scala and F # make this a lot easier, and C # hopefully follows.

In the meantime, I tried to overcome this obstacle by writing a T4 template that generates a constructor for you in a partial class, but after working with it for several weeks in the application, it really did not work as expected.

+2
source share

Since the languages ​​of the design / design pattern is neutral .

+2
source share

As Mr. Groddon notes in the comments: Function / method parameters are dependency injection - and almost all languages ​​support those at the lowest levels.

DI structures typically adapt to server environments. Language mechanisms would simply be the wrong level of abstraction for this.

+2
source share

In fact, they allow you to pass parameters to methods / constructors / functions - and that's almost all that DI frameworks do for it - just a fancy way of specifying parameter values.

A more interesting question would be how to apply dependency injection at the language level. Banning static is probably a good start (as Newspeak does).

+2
source share

This is inherently an erroneous approach. Ideally, languages ​​should be as subtle as possible, but powerful enough to introduce arbitrary abstractions at the language level (I think LISP and metaprogramming). If not, where do you draw the line between what to include and what to leave?

As for introducing compilation level support for DI, this is simply not possible. Your application may use dynamically linked libraries, and this may not be known even at compile time. Think of plugins.

+1
source share

If the language is flexible enough, it can mimic DI. Java and C # are clearly not, but functional or hybrid languages ​​often happen. For example. in Haskell, you can use Reader Monad to store the "environment" without clogging your code, or maybe you can use type classes. Scala has a mix and implicit objects that can be used for this. Therefore, I would conclude that a language that really needs DI simply does not have suitable and powerful abstraction mechanisms.

+1
source share

All Articles