Click here to Skip to main content
6,822,613 members and growing! (15,828 online)
Email Password   helpLost your password?
Languages » C# » Generics     Beginner License: The Code Project Open License (CPOL)

Generic Service Locator

By alanliu0110

This article shows a generic service locator implementation that gives you both ease of adding new services and type safe checking by compiler
Windows, Visual-Studio, Dev
Posted:30 Sep 2006
Views:14,643
Bookmarked:7 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.88 Rating: 3.12 out of 5

1
1 vote, 25.0%
2
1 vote, 25.0%
3
1 vote, 25.0%
4
1 vote, 25.0%
5

Introduction

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)];
    }
}

Conclusion

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.

References

History

  • 30th September, 2006: Initial post

License

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

About the Author

alanliu0110


Member

Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 30 Sep 2006
Editor: Deeksha Shenoy
Copyright 2006 by alanliu0110
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project