Method Injection Example

147 views
Skip to first unread message

Emad Ibrahim

unread,
Aug 21, 2008, 11:09:36 AM8/21/08
to ninject
I am writing a book on ASP.NET MVC and trying to choose a DI/IoC
framework for the book/application and I blogged about my search at
http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/. I am
thinking about using Ninject instead because of its ability to do
method injection.

But I need a better example...

The scenario I have is that I have an asp.net mvc application that has
a controller action that needs to
send email, so in my unit testing, I want to mock the email action.

So I created an interface IEmailService and added it as a parameter
like this:

public void ResetPassword(IEmailService emailer, string username,
string answer)

How would I inject IEmailService into this method when the user
navigates to www.mywebsite.com/account/resetpassword ???

Thanks.

Nate Kohari

unread,
Aug 21, 2008, 11:14:08 AM8/21/08
to nin...@googlegroups.com
Emad:

To inject dependencies into controllers, you should use either constructor or property injection. For example, using property injection, your controller might look like this:

public class AccountController : IController
{
  [Inject] public IEmailService EmailService { get; set; }
 
  public ActionResult ResetPassword(string username, string password)
  {
    //...
  }
}

I'd suggest you use the most-current version of Ninject from the trunk (which will soon become 1.5). It has an extension called Ninject.Framework.Mvc that plugs into ASP.NET MVC and causes your controllers to be activated via Ninject.


-Nate

Emad Ibrahim

unread,
Aug 21, 2008, 1:12:28 PM8/21/08
to ninject
Hi Nate,

I got construction injection and property injection working but I
still would like to know how to use method injection and when is a
good time to use method injection. The whole reason I am trying out
Ninject is method injection :)

Thanks.

On Aug 21, 11:14 am, "Nate Kohari" <nkoh...@gmail.com> wrote:
> Emad:
>
> To inject dependencies into controllers, you should use either constructor
> or property injection. For example, using property injection, your
> controller might look like this:
>
> public class AccountController : IController
> {
>   [Inject] public IEmailService EmailService { get; set; }
>
>   public ActionResult ResetPassword(string username, string password)
>   {
>     //...
>   }
>
> }
>
> I'd suggest you use the most-current version of Ninject from the trunk
> (which will soon become 1.5). It has an extension called
> Ninject.Framework.Mvc that plugs into ASP.NET MVC and causes your
> controllers to be activated via Ninject.
>
> -Nate
>

Nate Kohari

unread,
Aug 21, 2008, 1:13:44 PM8/21/08
to nin...@googlegroups.com
Emad:

Method injection won't work for ASP.NET MVC controllers... the arguments to action methods are expected to be built from the HTTP query string.


-Nate

Emad Ibrahim

unread,
Aug 21, 2008, 1:28:54 PM8/21/08
to ninject
Yea, I figured that out the hard way :)... I reverted to construction
injection and it is working beautiful.

My followup question is where, when and how to use method injection?
Maybe more details on this in the documentation and some examples
would help?

Thanks

On Aug 21, 1:13 pm, "Nate Kohari" <nkoh...@gmail.com> wrote:
> Emad:
>
> Method injection won't work for ASP.NET MVC controllers... the arguments to
> action methods are expected to be built from the HTTP query string.
>
> -Nate
>

Nate Kohari

unread,
Aug 21, 2008, 1:40:21 PM8/21/08
to nin...@googlegroups.com
Emad:

Method injection can be useful, but typically it's just used in the same way as property injection. I would favor constructor and property injection over method and field injection to be sure.

-Nate

Nicholas Blumhardt

unread,
Aug 21, 2008, 5:02:24 PM8/21/08
to ninject
I just revisited this question yesterday, too. Property injection is
definitely the way to go.

The .NET Framework Design Guidelines have a little snippet
recommending that properties never expose setters with less visibility
than getters - a SetXxxx() method is recommended in this case. I guess
that's where method injection would come into it.

On Aug 22, 3:40 am, "Nate Kohari" <nkoh...@gmail.com> wrote:
> Emad:
>
> Method injection can be useful, but typically it's just used in the same way
> as property injection. I would favor constructor and property injection over
> method and field injection to be sure.
>
> -Nate
>
Reply all
Reply to author
Forward
0 new messages