January 15, 2004Ioc typesI am a bit puzzled by Martin Fowler's description of the difference between IoC type 1 (interface injection): class MovieLister implements InjectFinder... and IoC type 2 (setter injection): class MovieLister... These two methods look exactly the same to me, except for the name of the method the initializes the component and the interface implemented by the first component (the second component could implement an interface as well). Am I missing something? Posted by cedric at January 15, 2004 04:14 PM Comments
Type 1 relies on casting vs Type 2 relies on reflection. The difference is mostly in the container implementation. Posted by: at January 15, 2004 05:46 PMThere is neither casting nor reflection in either of the two code snippets that I reproduced. Glad to see I'm not the only one confused. When container injects dependency in the first case it will be "instanceof" followed by the cast, the second case will produce code looking for a Method" and its signature. Posted by: Pinocio at January 15, 2004 08:00 PMIn the XWork implementation of IoC (very simple) instances are keyed off of the enabler Interface, as shown in what he's called Type 1, so I guess he'd call that Type 1. It basically just looks through the Interfaces implemented and looks for them in its registry. What I had previously heard referred to as Type 1 was the Avalon type config which ended up needing a dependency resolver to basically do service locator stuff to fulfill the dependency. Posted by: Jason Carreira at January 15, 2004 08:12 PMIt's actually fairly simple: with setters, you have no assurance that the setter will be called once and only once. Some idiot might come along and call the setter again. Or, if you're not using the framework, some idiot may not call the setter in the first place. By using the constructor, you _know_ that the object is correctly made, and that immutable properties won't change. Posted by: Robert Watkins at January 15, 2004 08:47 PMUnless, of course, someone calls the constructor with null values... Posted by: Jason Carreira at January 16, 2004 06:01 AMCedric, I share your confusion with the description of IoC 1 in Martin's article. According to the PicoContainer website, IoC type 1 (Contextualized Lookup, the new fancy schmancy name) pushes the dependency lookup and wiring down to the consumer service. (http://www.picocontainer.org/types-of-ioc.html) But, Martin's definition really doesn't illustrate this at all. His example, class MovieLister implements InjectFinder... Should really be something along the lines of, class MovieLister implements InjectFinder... Or, am I confused as well? :) (As a side note, although IoC is very generic and perhaps a tad ambiguous from a descriptive standpoint, injecting - pun intended - new terminology consisting of big long words I think only serves to raise the barriers to entry and scare off the bulk of would-be adopters.) Posted by: Joe Duffy at January 16, 2004 08:38 AMHmm, now that I think of it, "Inversion of Control" is pretty big, long, and scary as well... Posted by: Joe Duffy at January 16, 2004 08:40 AM"Type 1 relies on casting vs Type 2 relies on reflection. The difference is mostly in the container implementation." compared with this for Type 2: and the container would: correction for previous comment: <service id="movielister" class="MovieLister"> ServiceUtil.set(parentService, propName, dependentService); Cedric - I think that it is an over-categorization myself, but the main differences can be seen between SpringFramework and Avalon. The only concrete difference is the interface implementation, and more specifically, the existance of the interface itself. SpringFramework injects dependencies based on Javabeans specifications (and reflection), Avalon uses instanceof on the known interface types and casting. They are considered different because of this. Are you missing something? No. I think it is really people trying to segregate and seperate themselves - uniqueness goes a long way. Avalon is a non-reflection implementation, that's all. The main advantage of Type-2 is you don't have to mitigate IOC concerns into the class file (by implementing interfaces for the container). Isn't IOC, or whatever we're calling it here, a prime candidate for a "separation of concern" type of AOP solution. Basically, all the talk is having a push type of model to produce auto-wiring, auto-dependency, etc... Couldn't this whole problem be addressed through cross-cutting against the "new(..)" keyword. Let all the auto-wiring be triggered through this standard java keyword. A simple example: public aspect IOCAspect { pointcut creationAspect(): Object around():creationAspect(){ } Then have a Container: public class MovieLister { ... The container becomes "auto-wired" through the creationAspect advice without any restrictions on Setter or Constructor "Dependency Injections". It's simple. If you don't name the method inject, it will be boring Inversion of Control and you couldn't talk about it it. It's not romantic at all. Personally I would like weared Data Transfer Object to be renamed to Data Teleport Object. This alone will make RMI trasnfers instant. Posted by: Slava Imeshev at January 16, 2004 03:20 PMCedric, for me there is one big difference between IoC-2 (setter) and IoC-3 (constructor): After the constructor is called, the object has all its properties and is ready to be called in IoC-3. In IoC-2 the constructor is called and the object is (maybe) not ready to be called, because some dependencies are missing. Mirko Cedric, I think you're right to be confused; I am too. The crucial In fact, the only reason that the whole topic is interesting enough to His example of a Spring XML file is confusing because at first glance In the PicoContainer case, what am I supposed to assume that the pico.registerComponentImplementation(MovieFinder.class, ColonMovieFinder.class, finderParams); Does this mean that whenever anybody asks for an instance of When he starts talking about "service locators" he completely loses Post a comment
|