Why don't languages integrate Dependency Injection in their core (at the lowest level : compilation maybe) since dependencies are the root of all evil in Complexity Theory? This would avoid to use Frameworks.
I'm rather targeting compiled languages.
My question is similar to duck typing that has been introduced recently with dynamics in .NET. Why not something similar in the future for DI?
I personally can see the benefit of this. For instance, I'm writing code like this 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");
}
}
Wouldn't it be nice to be able to write it 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 wish I could just leave out all the plumbing with declaring my dependency fields and assigning them in my constructor.
UPDATE
Our prayers might have been heard. The C# team might be adding "succinct syntax for class definitions" such as us properties that "can be declared directly from the constructor" in C# 6.0. Let's hope such feature makes the light.
So your core question "Why don't languages integrate Dependency Injection at the core?", they do. Scala and F# do already make this much easier and C# will hopefully follow.
In the meantime I've tried to overcome this obstacle by writing a T4 template that generates the constructor for you in a partial class, but after working with this for a few weeks in an application it really didn't work out as expected.