If I want to use Ninject to create a service in my HomeController, I can do:

    IUserService _userService;

    public HomeController(IUserService userService)
    {
        _userService = userService;
    }

    public ActionResult SomeMethod()
    {
        userService.DoSomething();
       return View();
    }

    public ActionResult SomeOtherMethod()
    {
        //I don't use the service here
       return View();
    }

However, say I only need to use the service in just on of my controller methods (SomeMethod) - the constructor above fires for ALL methods in the controller (even SomeOtherMethod). Seems wasteful?

If I change it to the following, it works up until the view is returned, then I get an error (as detailed in Getting "MissingMethodException: Cannot create an instance of an interface" when binding generic interface to repository with Ninject):

    public HomeController()
    {
    }

    public ActionResult SomeMethod(IUserService userService)
    {
        userService.DoSomething();
       return View();
    }

Is it just that there's no overhead of putting ninject in the constructor - so it's OK? Or do I need one of the other ninject methods of Injection?

Thanks.

Related posts

Recent Viewed