user1152113
user1152113

Reputation:

Using DI and IoC with ASP.MVC 3 how do I setup my Controllers to resolve new types correctly?

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.

Upvotes: 3

Views: 597

Answers (2)

Jason
Jason

Reputation: 15931

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());
    }
}

Upvotes: 1

Carlos Mendes
Carlos Mendes

Reputation: 2000

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);
    }

    }

Upvotes: 2

Related Questions