This project is read-only.
1) Registering & accessing predefined service instance

var locator = new ServiceLocator();
var service = new SimpleService();   // implements ITestService interface

locator.Register(service); // all interfaces are of the instance are detected and registered

// since now on, the service should be available via number of ways
// let's try most of them if we receive the same object:
ITestService result = locator.GetService<ITestService>();

2) Accessing service object by name

string TestServiceName = "ServiceID:F587A062-1E7C-4358-A0AB-09032A954C69";
var locator = new ServiceLocator();
var service = new SimpleService();

locator.Register(TestServiceName, service);

// since now on, the service should be available via number of ways
// let's try most of them if we receive the same object:
ITestService result = locator.GetService<ITestService>(TestServiceName);

3) Access service created on a client request

var locator = new ServiceLocator();
ServiceCreationHandler h = (sender, e) =>
                               {
                                   e.Service = new SimpleService();
                               };

// perform service object delayed registration via handler as ITestService:
locator.Register(typeof (ITestService), ServiceMode.Singleton, h);

// then accessing it is as simple as it was before:
var service = locator.GetService<ITestService>();

4) Dynamic creation of service instances per request

var locator = new ServiceLocator();

// register one that per each request will generate new instance of service:
locator.Register(typeof(ITestService), typeof(SimpleService), ServiceMode.Clone);

// get reference to service objects, note that each service variable points to a new object:
var service1 = locator.GetService<ITestService>();
var service2 = locator.GetService<ITestService>();
var service3 = locator.GetService<ITestService>();

5) Bind the same dynamic service object to several service types or service names

var locator = new ServiceLocator();
var name1 = "MyService1";
var name2 = "MyService2";

// create any servie:
var r = locator.Register(typeof (ITestService), typeof (SimpleSiteService), ServiceMode.Singleton);

// try to bind with null:
locator.Bind(typeof (SimpleSiteService), r);
locator.Bind(name1, r);
locator.Bind(name2, r);

// get services, note that all service variables will point the the same object instance, that was lazily created:
var service1 = locator.GetService<ITestService>();
var service2 = locator.GetService<ITestService>(name1);
var service3 = locator.GetService<ITestService>(name2);

6) Cross ServiceLocator bindings
var s1 = new ServiceLocator();
var s2 = new ServiceLocator();

// register any service:
var r1 = s1.Register(typeof (ITestService), typeof (SimpleSiteService), ServiceMode.Singleton);

// try to bind in other locator:
var r2 = s2.Bind(typeof (ITestService), r1);

// since it's possible to share the same service across multiple service locators,
// expect the object to be identical:
var o1 = s1.GetService<ITestService>();
var o2 = s2.GetService<ITestService>();

Last edited Sep 24, 2010 at 11:23 PM by FeydRauth, version 1

Comments

No comments yet.