Ioc和WebForms-如何在用户控件中注入属性 [英] Ioc and WebForms - How to inject properties in user controls

查看:57
本文介绍了Ioc和WebForms-如何在用户控件中注入属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将IoC添加到已经存在的Web窗体项目中,并且在获取注入的用户控件(尤其是母版页中的动态用户控件)的依赖项方面遇到了一些麻烦.

I am adding IoC to an already existing web forms project, and I am having a little trouble with getting the dependencies of user controls injected, especially dynamic user controls in a master page.

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

On the master page, I load some user controls:

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);
    }

这些用户控件中的每一个都有一些依赖性.我可以在每个用户控件中手动注入依赖项:

Each of these user controls has some dependencies. I can manually inject the dependencies in each user control:

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

但这似乎违反了使用IoC的目的. MasterPage-> Page-> UserControls应该如何构造以允许DI?以及如何在动态用户控件中注入属性?我应该使用构造函数注入吗?

But that seems like it defeats the purpose of using an IoC. How should the MasterPage -> Page -> UserControls be structured to allow for DI? And how do I inject properties in a dynamic user control? Should I be using constructor injection?

我正在使用Autofac,但我认为此问题与IoC无关.

I am using Autofac, but I think this question id IoC agnostic.

推荐答案

您处在正确的轨道上.我通常创建一个 BaseUserControl 类,该类继承自 UserControl .然后从此 BaseUserControl 类继承所有 UserControls .

You are on the right track. I normally create a BaseUserControl class which inherits from UserControl. Then inherit all UserControls from this BaseUserControl class.

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

Place all required dependencies inside that BaseUserControl class.

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);
    }
}

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

You might think that it is a bit wasted, because not all user controls use all dependencies.

Mark Seemann在.NET书中的依赖注入"中指出,依赖注入速度非常快,而且从来都不是问题.如果您认为自己的应用程序运行缓慢,则可能来自代码的其他位置(而不是依赖注入).

Mark Seemann stated in Dependency Injection in .NET book that dependency injection is quite fast and never been an issue. If you think your application is slow, it will be from other places of your code (not from dependency injection).

如果要使用构造函数注入,请查看此 answer .

If you want to use constructor injection, look at this answer.

这篇关于Ioc和WebForms-如何在用户控件中注入属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆