Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject.Web and user controls

I am using the Ninject.Web library with our web forms application. It's working great except now I need to inject a dependency into a user control. What is the best way to accomplish this? Ninject.Web does not contain a base class like it does for web services, pages, and master pages.

like image 990
Chev Avatar asked May 19 '11 17:05

Chev


1 Answers

You can make a base class for user controls yourself:

public class NinjectedUserControl : System.Web.UI.UserControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        RequestActivation();
    }

    /// <summary>
    /// Asks the kernel to inject this instance.
    /// </summary>
    protected virtual void RequestActivation()
    {
        KernelContainer.Inject(this);
    }
}

I have Ninject.Web's source code in my solution and I have added this class to Ninject.Web (so it can access KernelContainer, which is internal).

like image 182
Paolo Falabella Avatar answered Oct 21 '22 21:10

Paolo Falabella