Reputation: 3279
Are there any existing modern-day programming languages that explicitly have dependency injection as a language feature, and if so, are there any examples of how such programming languages use their syntax to separate program dependencies from their concrete implementations?
(Please Note: I'm not looking for an DI/IOC framework--I'm actually looking for a programming language that actually has this feature built into the language).
Upvotes: 21
Views: 2215
Reputation: 49331
You won't find dependency injection as a language feature, as it's generally seen as a design pattern. Design patterns arise as workarounds for missing language features - for example if you have first class types as a language feature you don't need the factory pattern ( see p12 of Norvig's presentation ), if you have multi-methods as a language feature you don't need the double dispatch pattern.
The language feature for which DI is the design pattern is "parametric modules". See the discussion of modules vs DI relating to Gilad Bracha's language Newspeak
Upvotes: 13
Reputation: 282
One could say that Scala supports dependency injection out of the box with the help of traits and self-type annotations. Take a look on a Cake Pattern:
http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di/
Basically, this approach uses traits with declared dependencies (by using self-types) to let the compiler do the work of wiring them together.
This is the declaration registry:
object ComponentRegistry extends
UserServiceComponent with
UserRepositoryComponent
{
val userRepository = new UserRepository
val userService = new UserService
}
...registering the user repository:
trait UserRepositoryComponent {
val userRepository: UserRepository
class UserRepository {
...
}
}
...and the user service component that depends on the repository:
trait UserServiceComponent {
this: UserRepositoryComponent =>
val userService: UserService
class UserService {
...
}
}
Upvotes: 2
Reputation: 56832
Noop supposedly does this, but I haven't seen the language specification (my patience ran out before I found it).
Upvotes: 3
Reputation: 7016
I don't mean to sound like a jerk, but every OO language supports dependency injection. No special syntax is required. Just construct your object with their dependencies (or set their dependencies later).
You can actually wire up all your dependencies somewhere near the top of the program - not necessarily main()
, but close to the top.
Upvotes: 4