Ioc 和 WebForms - 如何在用户控件中注入属性



我正在将 IoC 添加到现有的 Web 窗体项目中,并且在注入用户控件的依赖项时遇到了一点麻烦,尤其是母版页中的动态用户控件。

在母版页上,我加载了一些用户控件:

protected override void OnLoad(EventArgs e)
    {
        bool processComplete = false;
        var page = Page as BasePage;
        if (page != null && HttpContext.Current.User.Identity.IsAuthenticated)
            processComplete = page.processComplete ;
        var segments = this.Request.Url.Segments;
        Control bodyheader = null;
        if (processComplete )
        {
            if (segments.Count() > 1 && segments[1].StartsWith("evaluation", true, System.Globalization.CultureInfo.CurrentCulture))
                bodyheader = Page.LoadControl("~/Themes/HWP/HeaderNoSearch.ascx");
            else
                bodyheader = Page.LoadControl("~/Themes/HWP/Header.ascx");
        }
        else if (segments.Count() > 1 && segments[1].StartsWith("welcome", true, System.Globalization.CultureInfo.CurrentCulture))
        {
            bodyheader = Page.LoadControl("~/Themes/HWP/plainHeaderAuth.ascx");
        }
        else
        {
            bodyheader = Page.LoadControl("~/Themes/HWP/plainHeaderAuth.ascx");
        }
        plcBodyHeader.Controls.Add(bodyheader);
        Control bodyfooter = Page.LoadControl("~/Themes/HWP/Footer.ascx");
        plcBodyFooter.Controls.Add(bodyfooter);
        base.OnLoad(e);
    }

其中每个用户控件都有一些依赖项。我可以在每个用户控件中手动注入依赖项:

protected override void OnInit(EventArgs e)
{
    var cpa = (IContainerProviderAccessor)HttpContext.Current.ApplicationInstance;
    var cp = cpa.ContainerProvider;
    cp.RequestLifetime.InjectProperties(this);
    base.OnInit(e);
}

但这似乎违背了使用 IoC 的目的。母版页 -> 页 -> 用户控件的结构应如何允许 DI?如何在动态用户控件中注入属性?我应该使用构造函数注入吗?

我正在使用Autofac,但我认为这个问题与IoC无关。

你走在正确的轨道上。我通常创建一个从UserControl继承的BaseUserControl类。然后从此 BaseUserControl 类继承所有用户控件

将所有必需的依赖项放在该 BaseUserControl 类中。

public class BaseUserControl : UserControl
{
    public ISomeService SomeService { get; set; }
    public IOtherService OtherService { get; set; }
    public BaseUserControl()
    {
        var cpa = (IContainerProviderAccessor)
            HttpContext.Current.ApplicationInstance;
        var cp = cpa.ContainerProvider;
        cp.RequestLifetime.InjectProperties(this);
    }
}

您可能会认为这有点浪费,因为并非所有用户控件都使用所有依赖项。

Mark Seemann 在 .NET 中的依赖注入一书中指出,依赖注入非常快,从来都不是问题。如果您认为您的应用程序很慢,它将来自代码的其他地方(而不是来自依赖项注入)。

如果你想使用构造函数注入,看看这个答案。

最新更新