![]() |
Languages »
C# »
Generics
Beginner
License: The Code Project Open License (CPOL)
Generic Service LocatorBy alanliu0110This 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
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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?
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.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 30 Sep 2006 Editor: Deeksha Shenoy |
Copyright 2006 by alanliu0110 Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |