User loginNavigation |
archivesDependency injection via parameterized types (?!)Say we have a C#-like language that keeps runtime information about type arguments. Then we can say something like:
class Dependent<T> {
void doIt() { new T(); }
}
class Dependency {
}
// "wire" our cool dependency network
Dependent d = new Dependent<Dependency>();
// creates a new instance of the Dependency
d.doIt();
Doesn't this subsume almost all applications of DI? The top-level of an application then becomes a big NEW expression, that wires the DI network. For mock-testing, one would use a different statement: // normal application new MyApplication<MyService1Impl, MyService2Impl, ...>(); // for testing: new MyApplication<MyService1Mock, MyService2Mock, ...>(); Now, I can see that sometimes one doesn't want to create a fresh instance of some dependency, but rather get an existing instance from an "object broker", so plain NEW is the wrong thing. But many object-oriented Lisp dialects already have a solution for this, too: NEW is simply a virtual function, that can be overridden on a per-class basis [e.g. Dylan's make]. This allows one to actually interpret any "new T()" expression as an ordinary call of a function, that could then use reflection to get an instance of the actual class T from the broker. Am I missing something here, or do parameterized types offer a full solution to dependency injection? |
Browse archivesActive forum topics |
Recent comments
1 day 23 hours ago
2 days 20 hours ago
4 days 58 min ago
4 days 1 hour ago
1 week 2 days ago
1 week 2 days ago
1 week 2 days ago
4 weeks 2 days ago
5 weeks 1 day ago
5 weeks 1 day ago