3

I’ve been reading Mark Seemann’s book Dependency Injection in .NET and his blog post regarding DI Composition Roots.

And I’m in the process of refactoring my ASP.MVC 3 application Container calls out of my controllers and into a compositional root.

In the past I’d resolve types inside my controller like this.

[HttpGet]
public ActionResult Create()
{
    // Don't do this...
    var color = MvcApplication.Container.Resolve<IColor>();                           
    return View(color);
}

After reading Mark’s book I realize this is less then ideal so I’ve setup my MVC application to inject a repository into the controller on app start using a class derived from the IControllerFactory as dicussed in Mark’s book in section 7.2.1.

If I’m not supposed to call an IoC container directly from my ASP.MVC 3 Controller using the Service Locator pattern how and where do I instantiate new types? Each controller is different so I want a generic way of resolving new types.

I could instantiate types directly like this but that would kind of defeat the purpose.

[HttpGet]
public ActionResult Create()
{
   // Don't do this...
   var color = new Color();
   return View(color);
}

Should I be injecting a Generic Factory into my Controller along side the Generic Repository and use it to new create types? Is there a better way?

EDIT

I realize this is similar to another SO question but my entities have dependencies that need to be resolved before passing them on to a view.

Community
  • 1
  • 1
  • 1
    Related: http://stackoverflow.com/questions/1943576/is-there-a-pattern-for-initializing-objects-created-via-a-di-container/1945023#1945023 – Mark Seemann Jan 22 '12 at 09:04
  • 1
    Related: http://stackoverflow.com/questions/4835046/why-not-use-an-ioc-container-to-resolve-dependencies-for-entities-business-objec/4836790#4836790 – Mark Seemann Jan 22 '12 at 09:04
  • 1
    @Mark Seemann Just found your book last weekend and couldn't put it down - great work! I'll work in the abstract factory as you suggested. –  Jan 22 '12 at 16:09

2 Answers2

2

There's some info about the Service Locator usage in this question.

You can always deal with the dependency through the Controller constructor:

 public class HomeController : Controller
    {

    private readonly IColor _iColor;

    public HomeController(IColor iColor)
    {

    _iColorService = iColor;
    }

    [HttpGet]
    public ActionResult Create()
    {
       var color = _iColor;
       return View(color);
    }

    }
Community
  • 1
  • 1
Carlos Mendes
  • 1,900
  • 1
  • 18
  • 33
  • This is the better answer since the IoC container should be the factory for all the dependencies. Just make sure to use a well featured DI framework like StructureMap, Windsor or similar, and register the dependencies properly. – Dmitry S. Jan 23 '12 at 06:52
  • 1
    @Beller I'm not sure about your usage Scenario but if you take a look at the DI Patters section of the book (starting page 95) you'll see that this one (Constructor Injection) is the most common. – Carlos Mendes Jan 23 '12 at 17:23
1

You want to create a container and register services in global.asax (including your controllers) when you application starts up. You also want to provide your own implementation for IControllerFactory that uses the container I just described. Inform the application to use your new controller factory by setting the proprty ControllerBuilder.Current.SetControllerFactory.

More details here: http://bradwilson.typepad.com/blog/2010/07/service-location-pt2-controllers.html

You don't want to new up objects in your controller actions, rather, your controllers should take dependencies on services that can provide the objects it needs.

One approach would be the following

public interface IColorFactory
{
  IColor GetColor();
}

// this implementation is registered with the container
public class DefaultColorFactory : IColorFactory
{
  // this implementation is the only one that knows the details of the IColor implementation
  public IColor GetColor() {  return new Color(); } 
}

public class MyController : ControllerBase
{
  private readonly IColorFactory _colorFactory;
  public MyController(IColorFactory cf)
  {
    _colorFactory = cf;
  }

    [HttpGet]
    public ActionResult Create()
    {
      return View(_colorFactory.GetColor());
    }
}
Jason
  • 15,915
  • 3
  • 48
  • 72
  • My question is not how do I set up a compositional root in MVC rather how do I maintain the integrity of the compositional root when I need to new up objects in side my controller actions. Mark's book explains how to set it up - I'm good there. –  Jan 22 '12 at 03:39
  • Based on Mark Seemann's and your answer I'm going to go ahead and use the factory pattern as suggested. –  Jan 22 '12 at 16:08