Tuesday, August 16, 2011

using ServiceLocator in a model

Here is an example of using ServiceLocator in a model:

using Something.SomethingElse;

namespace Whatever

{

   public class Foo

   {

      public IBarRepository BarRepository { get; set; }

      public IBazRepository BazRepository { get; set; }

      

      public Foo()

      {

         BarRepository = ServiceLocator.Get<IBarRepository>(AppCtxIds.BAR);

         BazRepository = ServiceLocator.Get<IBazRepository>(AppCtxIds.BAZ);

         CurrentTime = DateService.Now();

      }

      

      public Foo(DateTime myTime)

      {

         CurrentTime = myTime;

      }

      

      
More code here...

 
 

In our code base, one has to have...

  1. Public get/setters for repositories
  2. Parameterless constructors for instantiation (I don't fully understand why as of yet)

 
 

The different constructor here is for testing a specific time value from two unit tests that will not need the repositories.

No comments:

Post a Comment