Dependency Injection: Introducing User Control on an Aspx Page

I need to create a custom asp.net web application (not asp.net mvc).

I was thinking of using an IoC container to inject a user control into aspx pages.

Has anyone ever tried this?

In short, this is how I think this can be achieved:

  • I use the Scott Guthrie method to create a reusable "user management library" (see his article, "Creating and Using User Management Libraries").

  • The controls and pages in this library must have room to dynamically retrieve custom controls (for example, in asp PlaceHolder).

  • In my custom web application built on top of this library, I create specific user controls.

  • I put them somehow in an IoC container so that they can be entered into the controls / pages of the "user control library" (for example, in PlaceHolders).

What is it.

This is basically what you can do with Spring.Net, as discussed here at http://blogger.forgottenskies.com/?p=70 , but for entering custom controls.

Does anyone have experience with such things? Or does that sound silly? Alternatives?

The idea is to be able to create various web applications on top of my "user management library" without touching this library.

: 4 , . , .

+5
2

, , . , :

public interface IEditor
{
    bool CanHandle(EntityBase entity);
    void Display(EntityBase entity);
    void Save(EntityBase entity);
}

public partial class AddressEditor : UserControl, IEditor
{
    public bool CanHandle(EntityBase entity)
    { 
        return (entity is Address);
    }

    public void Display(EntityBase entity)
    {
        var address = (Address)entity;
        addressLine1Textbox.Text = address.Line1;
        // etc...
    }

    public void Save(EntityBase entity)
    {
        var address = (Address)entity;
        address.Line1 = addressLine1Textbox.Text;
        // etc...
    }
}

, IoC (StructureMap ), -

var editorControl = ObjectFactory.GetAllInstancesOf<IEditor>().First(x => x.CanHandle(myEntity));

, , . .ascx. , :

public string AscxFile { get { return "~/UserControls/AddressEditor.ascx"; } } // Implements IEditor.AscxFile

LoadControl:

var actualControl = LoadControl(editorControl.AscxFile);
editorPlaceholder.Controls.Add(actualControl);
editorControl.Display(myEntity);

Composite Controls, , .ascx, :

var editorControl = ObjectFactory.GetAllInstancesOf<IEditor>().First(x => x.CanHandle(myEntity));
editorPlaceholder.Controls.Add((Control)editorControl);
editorControl.Display(myEntity);

, Page.OnInit, ViewState, postbacks, .

IoC, StructureMap IEditor, xml.

+4

.
PageBuilder, , UserControls WebParts.

( , , ..), - , UserControls WebParts. ( /).

, SQL-, , ..

:

  • .
  • -.
  • UserControl WebPart .
  • UserControl WebPart.

WebParts /. WebPart A = > WebPart B .

, .

, , , , . , .

Edit:
, PageBuilder.
: , UserControls, WebPart, ..
aspx PlaceHolder UserControls WebParts. PlaceHolder PageBuilder.

UserControl/WebPart PlaceHolder aspx , . aspx . , . UserControl WebPart. , aspx , .

100+ UserControls WebParts aspx, aspx :

<%@ Page MasterPageFile="main.master" ... %>
<asp:Content runat="server" ContentPlaceHolderID="Main" ID="MainSection">
  <asp:PlaceHolder runat="server ID="UserControlPlaceHolder"></asp:PlaceHolder>
</asp:Content>

codebehind - :

Partial Class MyPageClass Inherits BasePage
  Protected Sub Page_Init(ByVal sender As Object, ByVal e as System.EventArgs) Handles Me.Init
    'The following method is in the BasePage and is part of the PageBuilder.
    LoadControls()
  End Sub
End Class

PageBuilder , - .
(, , .)

PageBuilder -. . , , " " (, , ..).

, .

+1

All Articles