Thursday, August 11, 2011

How Spring.NET Service Locator works

Some objects have parameterless constructors in them like this:

public SampleModel()

{

   FooRepository = ServiceLocator.Get<IFooRepository>(AppCtxIds.FOO_REPOSITORY);

}

 
AppCtxIds looks like this:

public static class AppCtxIds

{

   public const string FOO_REPOSITORY = "fooRepository";

}

 
fooRepository will be matched, I assume, against an item in one of the .xml files mentioned here.

 
 

Addendum on 11/18/2012: I wrote this blog posting some time ago when I was first learning Spring.NET. Service Locator is different from Dependency Injection in that while Dependency Injection uses XML mappings to associate classes to interfaces across project boundaries, Service Locator does not. It instead crawls the whole of the application context and tries to make matches based on naming convention. Service Locator and Dependency Injection are the two varieties of Inversion of Control.

No comments:

Post a Comment