Ioc and WebForms - How to inject properties into custom controls

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

The home page loads some custom 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 dependencies into each custom control:

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

      

But it looks like it defeats the purpose of using IoC. How to structure MasterPage -> Page -> UserControls structure to allow DI? And how do I add properties to a dynamic custom control? Should I be using constructor injection?

I am using Autofac but I think this question is IoC independent.

+3


source to share


1 answer


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

Place all required dependencies inside this 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 this is a bit lost because not all custom controls use all dependencies.

Mark Semann stated in the book "Dependency Injection" in the .NET book that dependency injection is pretty fast and has never been a problem. If you think your application is slow, it will originate from elsewhere in your code (not dependency injection).

If you want to use the constructor installation take a look at this answer .

+3


source







All Articles