views:

201

answers:

1

I created a ASP.NET user control, and was wanting to create a factory with a method that allowed a control to be created via several parameters.

Originally I was going to have a constructor on the user control with the parameters, but the parameters are really only provided to make creating the user control easier from some legacy code I don't want to change significantly. I decided I wanted to instead have a method in a factory class that the legacy code could use, that way I don't muddy up the interface of my user control.

The problem I have is I can't reference the user control class from the factory class(since normally you reference a user control programmatically by adding <%@ Reference Control="~/SomeControl.ascx" %> but the factory class isn't a asp page).

On a side note I was going to have the calling page pass a reference to itself so that the factory method could call LoadControl, but I still need to be able to declare a SomeControl variable, which it cannot.

How do I create a factory for a user control?

A: 

Declarative programming (for which User Controls are designed) doesn't translate well into imperative object construction. Why not create an adapter User Control (or server control) that makes SomeControl easier to use by the legacy code? Like the following. Say we have a legacy page that has a lot of logic in terms of SqlDateTime, and we write some new control that does its work in terms of plain-old DateTime.

LegacyPage.aspx:

<%@ Page Language="C#" AutoEventWireup="true"  %>
<%@ Register Src="~/SomeControlLegacyAdapter.ascx" TagName="SomeControlLegacyAdapter" TagPrefix="test" %>
<script type="text/C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        SomeControlLegacyAdatper1.Time = new System.Data.SqlTypes.SqlDateTime(DateTime.Now);
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <test:SomeControlLegacyAdapter runat="server" ID="SomeControlLegacyAdatper1" />
    </div>
    </form>
</body>
</html>

SomeControlLegacyAdapter.ascx

<%@ Control Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data.SqlTypes"%>
<%@ Register Src="~/SomeControl.ascx" TagPrefix="test" TagName="SomeControl" %>

<script type="text/C#" runat="server">
    public SqlDateTime Time
    {
        get { return SomeControl1.Time; }
        set { SomeControl1.Time = (DateTime)value; }
    }
</script>

<test:SomeControl ID="SomeControl1" runat="server" />

SomeControl.aspx

<%@ Control Language="C#" AutoEventWireup="true" %>
<script type="text/C#" runat="server">
    public DateTime Time
    {
        get { return string.IsNullOrEmpty(output.Text) ? DateTime.MinValue : DateTime.Parse(output.Text); }
        set { output.Text = value.ToString(); }
    }
</script>

<asp:Literal runat="server" id="output" />

The legacy adapter converts SomeControl into a form easier for LegacyPage to consume.

gWiz