Tuesday, September 17, 2013

Reinventing the wheel, actually Ninject

I was doing some dynamic invoking work with Asp.Net WebApi.

Code worked from beginning, middle, and end. The middle was a bunch of ugly unnecessary if statements that needed manual configuration for each class.  I didn't have time to use (fight) reflection and assembly invoke.

First Refactor

Custom service locator / factory.  Good old inversion of control.

I stubbed out a few classes.  Wired up the first item, then realized...

Realization I'm reinventing the wheel!

Then saw that I already had this in Ninject! I just needed to add create an interface for each controller class.

My custom built service locator / factory became

public virtual IAbstractXrxController ManualControllerFactory(Type controllerType)
{
      var ctrl = (IAbstractXrxController)GlobalConfiguration
                    .Configuration.DependencyResolver
                    .BeginScope().GetService(controllerType);
      return ctrl;
}



I used http://tohtml.com/ to create html code.

No comments:

Post a Comment