Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > Other ASP.NET > BOOK: Professional ASP.NET Design Patterns
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET Design Patterns
This is the forum to discuss the Wrox book Professional ASP.NET Design Patterns by Scott Millett; ISBN: 978-0-470-29278-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET Design Patterns section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 27th, 2011, 07:56 AM
Registered User
 
Join Date: Jan 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default IoC Container vs Service Locator

Dear Scott,

When discussing IoC Containers in chapter 8, you mention that your client code does not have to know about concrete implementations and it gets the right implementation injected.

I don't really see it I guess. You still need to wire up the dependencies in the Bootstrapper, which resides somewhere in the client. Actually the whole Resolve method looks like a Service Locator / Factory to me. You just specify that you want the concrete implementation of some interface and you will get it. How it knows what to get is in the bootstrapper.
The main difference is that by writing a Service Locator yourself, you have all the wiring code in the Locator and need a bit more work maybe, as with an IoC container you don't need to write the Locator, but just set it up. But both are somewhere in your client code.

Am I missing something?

What I do see is the auto-wireup of a dependency chain, which is very neat and does feel like a plus.
Reply With Quote
  #2 (permalink)  
Old January 27th, 2011, 08:48 AM
elbandit's Avatar
Wrox Author
Points: 599, Level: 8
Points: 599, Level: 8 Points: 599, Level: 8 Points: 599, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2007
Location: Southsea, Portsmouth, Hampshire, United Kingdom.
Posts: 107
Thanks: 10
Thanked 17 Times in 15 Posts
Default

Hi MrFixxiT,

First of all thanks for buying the book!

Ok I my knowledge on the ServiceLocator pattern came from the article by Fowler http://martinfowler.com/articles/injection.html which if you have a read should answer your question. Basically the service locator does not have the ability to auto wire up dependencies, it acts more like a registry for services with parameterless constructors. An IoC container on the other hand can create a concrete class that does rely on other interfaces by auto wiring up with appropriate implementations that it has in its registery.

In his article an IoC can resolve the MovieLister class...

class MovieLister...
public MovieLister(MovieFinder finder) {
this.finder = finder;
}


MovieLister lister = (MovieLister) pico.getComponentInstance(MovieLister.class);

The MovieFinder class is auto wired up. When he uses the ServiceLocator, he uses the code...

class MovieLister...
MovieFinder finder = ServiceLocator.movieFinder();


And has to specifically ask for a movie finder. So therefor the class is asking for its dependencies, the IoC container inverts this flow of control and instead of the MovieLister class asking it is instead told.

Either way the main thing is to keep your classes loosely coupled by coding to abstractions.

I hope this has answered your question. If not please let me know.

Cheers
Scott
Reply With Quote
  #3 (permalink)  
Old January 27th, 2011, 09:01 AM
Registered User
 
Join Date: Jan 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for answering, but... :)

In that example you (or Fowler) uses the Locator inside the MovieLister class. Maybe that's when it's offcially called a Service Locator. If you still use DI via the constructor for the MovieLister class, like in the first example, and then use a Locator when instancing the MovieLister class to insert all the dependecies into the MovieLister class, it's almost the same as with the IoC container. The only difference being that you have to write all of the Service Locator yourself.

The advantage has to do with the location of asking for the concrete implementation.

And like I said, the auto-wiring is cool and a genuine advantage over writing heave Service Locator classes that do the dependency chains by themselves.
Reply With Quote
  #4 (permalink)  
Old January 27th, 2011, 09:13 AM
elbandit's Avatar
Wrox Author
Points: 599, Level: 8
Points: 599, Level: 8 Points: 599, Level: 8 Points: 599, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2007
Location: Southsea, Portsmouth, Hampshire, United Kingdom.
Posts: 107
Thanks: 10
Thanked 17 Times in 15 Posts
Default

I think traditionally a service locater used with a service that has a constructor would look like:

var movie_service = ServiceLocator.Locate<IMovieService>("with_IMDB_fi nder");

or

var movie_service = ServiceLocator.Locate<IMovieService>(FinderType.Am azon);

i.e.you are asking the service for what finder you want it to use. Its a very subtle difference, and may have been blurred over time, plus I think people use the term interchangeably.

But yes auto wiring is cool !

Cheers
Scott
Reply With Quote
  #5 (permalink)  
Old January 27th, 2011, 11:10 AM
Registered User
 
Join Date: Jan 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Which you can do with StructureMap for example, where you can specify multiple concrete implementations for an interface, identified by a string like that.
I guess that part of an IoC container (or at least StructureMap) is really (a lot like) a Service Locator.

But you did make things clearer. Thanks very much. Best book I read for quite some time by the way. Keep up the good work.

I think you have a very valid point with the blurring of definitions over time. It's good we have names for design patterns, so we have a vocabulary to talk about them, but I guess at least some of them have been miss-interpreted along the way resulting in a couple of mismatches between what most people think now and what the original definition is (or has been). Most of the time discussions about these things are just syntactical, and we should have a better look at what we're trying to do. :)
Reply With Quote
  #6 (permalink)  
Old January 27th, 2011, 05:23 PM
elbandit's Avatar
Wrox Author
Points: 599, Level: 8
Points: 599, Level: 8 Points: 599, Level: 8 Points: 599, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2007
Location: Southsea, Portsmouth, Hampshire, United Kingdom.
Posts: 107
Thanks: 10
Thanked 17 Times in 15 Posts
Default

Thanks MrFixxi, don't suppose you want to add a review on Amazon of how you feel about the book
Reply With Quote
  #7 (permalink)  
Old January 31st, 2011, 02:01 AM
Registered User
 
Join Date: Jan 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I might. Never done it before, but will have a look into it when I get some time.
But do you need it? You've got lots of wonderful reviews already!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IoC container for ProductService without registry entry? samvan BOOK: Professional ASP.NET Design Patterns 5 November 25th, 2010 01:39 PM
What is the difference between AOP and IoC kotaiah Spring 0 September 6th, 2007 01:21 AM
what the heck is a container?[:D] lcsgeek General .NET 0 January 24th, 2005 12:56 PM
Name 'Container' is not declared. surendran ASP.NET 1.0 and 1.1 Professional 1 August 17th, 2004 10:33 AM
Form Container shortynelson General .NET 1 August 13th, 2004 01:27 PM



All times are GMT -4. The time now is 01:38 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.