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
22 weeks 4 days ago
22 weeks 4 days ago
22 weeks 4 days ago
44 weeks 5 days ago
49 weeks 18 hours ago
50 weeks 4 days ago
50 weeks 4 days ago
1 year 1 week ago
1 year 5 weeks ago
1 year 5 weeks ago