Click here to Skip to main content
11,922,788 members (49,452 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked

Generic Service Locator

, 30 Sep 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
This article shows a generic service locator implementation that gives you both ease of adding new services and type safe checking by compiler


In general, there are two approaches to implement the Service Locator pattern. The first is like the following:

public class ServiceLocator
    public void AddService(String ServiceName, IService Service){...}
    public IService GetService(String ServiceName){...}        

You can imagine what the client code looks like.

IOrderSvc OrderSvc = (IOrderSvc) ServiceLocator.GetInstance().GetService(ORDER_SVC);

The other approach is like this:

public class ServiceLocator
    public void addService(String ServiceName, IService Service){...} 
    public OrderSvc getOrderSvc(){...}
    public AccountSvc getAccountSvc(){...}

Both approaches have their pros and cons. The first approach allows you to add new services easily. The two methods are pretty much it for the ServiceLocator. You create the new service class and call the AddService to add the new service to the locator. But you get the benefit at the expense of type safety. The second approach is exactly the other way around. You have type safety but each time you add a new service, you need to add a new GetXXSvc to the ServiceLocator. As time goes on, the ServiceLocator becomes long and tedious. Is there any way to have the benefits of both approaches?

Generic Methods

The code for the second approach shows a pattern:

public T GetT(){...}

The pattern is exactly what generics are designed for to eliminate the duplication. The following is the new ServiceLocator implementation using generic methods:

public class ServiceFactory
    private Hashtable services = new Hashtable();
    public void AddService<T>(T t) where T:IService
        services.Add(typeof(T), t);
    public T GetService<T>()
        return (T)services[typeof(T)];


No changes to the new ServiceFactory will be needed when adding a new service. We also have type safe checking by compiler when getting a service from this ServiceFactory.



  • 30th September, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151125.1 | Last Updated 30 Sep 2006
Article Copyright 2006 by alanliu0110
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid