Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection as a Language Feature?

Tags:

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).

like image 517
plaureano Avatar asked Oct 03 '09 06:10

plaureano


People also ask

What is dependency injection in a programming language?

In object-oriented programming (OOP) software design, dependency injection (DI) is the process of supplying a resource that a given piece of code requires. The required resource, which is often a component of the application itself, is called a dependency.

What is dependency injection features?

The 4 roles in dependency injection These are: The service you want to use. The client that uses the service. An interface that's used by the client and implemented by the service. The injector which creates a service instance and injects it into the client.

What is dependency injection with examples?

What is dependency injection? Classes often require references to other classes. For example, a Car class might need a reference to an Engine class. These required classes are called dependencies, and in this example the Car class is dependent on having an instance of the Engine class to run.


2 Answers

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

like image 102
Pete Kirkham Avatar answered Oct 14 '22 11:10

Pete Kirkham


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.

like image 40
Daniel Yankowsky Avatar answered Oct 14 '22 12:10

Daniel Yankowsky