Print

Simple Service Locator - The easiest Inversion of Control framework in town

The Simple Service Locator is an easy-to-use Inversion of Control library that is a complete implementation of the Common Service Locator interface. It solely supports code-based configuration and is an ideal starting point for developers unfamiliar with larger IoC / DI libraries

Download: The Simple Service Locator library and source code can be downloaded from CodePlex.com. Visit the homepage at simpleservicelocator.codeplex.com or go directly to the Downloads tab.

Injection of uuhhm.. dependenciesMany development teams I help have legacy code bases with little or no unit tests. As you can imagine, the complexity makes it hard to add new features and fix existing bugs. Changing the course is often not easy, because the developers need to learn a whole new back of tricks. One of those tricks is unit testing and to me inextricably connected with that is dependency injection.

To get teams on the road quickly I often want to do the simplest thing that could possibly work. For that reason I've been annoyed with the complexity of the popular inversion of control frameworks. The concepts of inversion of control (IoC), dependency injection and good RTM unit tests are by itself hard enough to gasp for many developers. Learning to work with and configure several different frameworks (such as logging, validation, O/RM, and dependency injection) at the same time makes it even harder.

For this reason I started the Simple Service Locator project on CodePlex. It’s a (yet another) IoC library for .NET. Key features are its simplicity and it being an implementation of the Common Service Locator interface. This makes it especially useful for development teams unfamiliar with one of the existing IoC frameworks. Development teams can start using the Simple Service Locator and replace it with a more feature-rich IoC framework later on when needed, without having to alter any production code. For the lifetime of many projects however, I expect the Simple Service Locator just to be sufficient.

Simple Service Locator is an implementation of the Common Service Locator (CSL) interface and is not meant to be used without it. Therefore production code should only call CSL's ServiceLocator facade, as is shown in the following example:

IWeapon weapon = ServiceLocator.Current.GetInstance<IWeapon>();

Configuring the Simple Service Locator is done in the startup path of the application, as can be seen below. In this example you see how the Simple Service Locator is configured in the Application_Start event of the global.asax of a ASP.NET web application.

using System;
using CuttingEdge.ServiceLocation;
using Microsoft.Practices.ServiceLocation;

public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
// 1. Create a new Simple Service Locator container
var container = new SimpleServiceLocator();

// 2. Configure the container

// Register a delegate that will create a new
// instance on each call to GetInstance<Warrior>.
container.Register<Warrior>(() =>
{
var weapon = container.GetInstance<IWeapon>();
return new Samurai(weapon);
});

// Register a single object instance that always
// be returned (must be thread-safe).
container.RegisterSingle<IWeapon>(new Katana());

// 3. Register the container to the Common Locator
ServiceLocator.SetLocatorProvider(() => container);
}
}

Please visit the Simple Service Locator home page to see more code examples.

The project is currently in beta, which means your feedback is very welcome. What do you think of the current API? Can we make it even simpler? Do you miss anything? I like to know.

Cheers.

- .NET General, C#, Dependency injection, Simple Service Locator - eight comments / No trackbacks - §

The code samples on my weblog are colorized using javascript, but you disabled javascript (for my website) on your browser. If you're interested in viewing the posted code snippets in color, please enable javascript.

eight comments:

cool, this may get some newbies to use DI, we need more of this type of stuff to introduce the concepts :)
what's with the partial RSS dude!?
Eber Irigoyen (URL) - 06 01 10 - 18:34

Hey Eber, I use a partial RSS because the feed would become too large. In comparison, the rss.xml is currently 53KB, but a full feed would be 482KB, which is too large. I would have almost 1GB of traffic a month for just the rss.xml!
Steven (URL) - 06 01 10 - 20:46

Your library really works well. I don't think it will get any easer than this ;-)
MarcelXD - 08 01 10 - 10:07

Service Locator != Dependency Injection, for the simple reason that there is no injection happening. DI is push, SL is pull.

I know it's pedantic, but the concepts surrounding composition are similar enough to require such distinction.
Bryan Watts - 10 01 10 - 07:56

Bryan, you are right that the dependency injection pattern is not the same as the service locator pattern (however, closely related). I picked this name because of two important features of the library: 1. It's an implementation of the Common Service Locator. 2. It is simple! :-)
Steven (URL) - 19 01 10 - 11:41

Fair enough. I was just trying to say that what you have isn't a dependency injection library, and calling it that can potentially be confusing for your target audience (teams just getting started with IoC).

Moving to a dependency injection library from a service locator library includes removing all references to the static service locator instance. Once those references are gone, depedencies can be injected instead of requested, and DI is in effect. It is not in effect for a service locator (but IoC is).
Bryan Watts - 19 01 10 - 16:37

Okay, I see your point. You are right, the "Simple Service Locator" is an IoC framework and it can be used for both the Dependency Injection and Service Locator patterns. So, a better title for this post would have been: "Simple Service Locator - The easiest Inversion of Control framework in town". I fixed this. Thanks for your remarks.
Steven (URL) - 20 01 10 - 22:37

Thank you for this library. I needed something simple that implemented the Common Service Locator. Did the job.
cwrea - 10 08 10 - 03:41