View Full Version : "don't use spring as just a service locator"
MNBob
09-23-2005, 02:06 PM
I've read comments that have said not to just use spring as a service locator. The comments go on to say it shouldn't be necessary to call GetObject and to instead inject the container.
Can someone point me to some examples/documenation that shows how this is done?
Thanks.
Mark Pollack
09-23-2005, 03:30 PM
Hi Bob,
The reference docs are less of a how-to then a relatively dry description of functionality. You will probably find the documentation of the QuickStart Examples (http://www.springframework.net/examples.html) included in the distribution as a practical way to explore using Dependency Injection.
In particular, the MovieFinder (http://www.springframework.net/doc/reference/html/quickstarts.html) example shows dependency injection of SimpleMovieFinder (which implements the interface IMovieFinder) 'into' MovieLister.
<object name="MyMovieLister"
type="Spring.Examples.MovieFinder.MovieLister, Spring.Examples.MovieFinder">
<property name="movieFinder" ref="MyMovieFinder"/>
</object>
<object name="MyMovieFinder"
type="Spring.Examples.MovieFinder.SimpleMovieFinder, Spring.Examples.MovieFinder"/>
</object>
The key line here is
<property name="movieFinder" ref="MyMovieFinder"/>
This instructs the Spring container to set the MovieFinder property with the given reference after it intantiates MovieLister.
You may also check out a recent MSDN article (http://msdn.microsoft.com/msdnmag/issues/05/09/DesignPatterns/default.aspx) that shows the use of Dependency Injection with Spring.NET. There are also many books, articles, and presentations (http://www.springframework.org/documentation)that discuss using Dependency Injection with Spring.Java. These can be used to get started with Spring.NET as well, just translate the use of bean to object and you are most of the way there.
Cheers,
Mark
MNBob
09-23-2005, 04:39 PM
Thanks for the reply Mark.
I'm already using Spring.NET to inject web service implementations in the project I'm working on.
My question deals specifically with how to create objects without using GetObject. In your example, how would "MyMovieLister" be created without using GetObject?
I believe some documentation mentions that the web implementation takes care of the container creation and that similar functionality for windows forms applications is being investigated.
I'm just looking for an example.
Mark Pollack
09-23-2005, 05:07 PM
Hi,
In the case of web services the IIS host is actually responsible for creating the object. We plug into that creation mechanism when you access a web service endpoint. Same for the support of ASP.NET pages. For these cases you never see 'GetObject'. If it is a stand alone application, say as in the case of the MovieFinder example, you will typically need to call GetObject on a root/top level object and then work with it. Everything else connected to that object should ideally be configured using DI to obtain references. MovieFinder is just a toy example that really doesn't to anything useful, just to demonstrate DI.
In a messaging application you can have some custom messaging factory objects that use DI to configure the messaging infrastructure - instructing it what message queues to listen on and the callback method. The callback method in turn would have other dependencies. The Main of the app would then just initialize the context, pull out a top level messaging class, and then call 'start listening' on it.
For WinForm applications you tend to see more use of GetObject in reaction to creating objects based on runtime user actions or to get some resource like an image or text message. You can use for example the ConfigureObject instead of GetObject in a WinForm Load event to configure the view's controller (if using MVC) and in turn the controller may reference some service class that gets information from a remote machine. If you don't have any nested user controls you can use Spring to configure the WinForm directly. Only in the case of nested user controls, where the code generated by the IDE is not Spring aware, do you really need to go down the ConfigureObject route. We are investigating having a base class for WinForms that can handle this elegantly.
In short, different environments will require a different mix of service locator style.
Hope this helps.
Cheers,
Mark
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.