开发者

Ninject.Web and user controls

开发者 https://www.devze.com 2023-03-07 06:05 出处:网络
I am using the Ninject.Web library with our web 开发者_如何学Goforms application. It\'s working great except now I need to inject a dependency into a user control. What is the best way to accomplish t

I am using the Ninject.Web library with our web 开发者_如何学Goforms 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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消