views:

126

answers:

3

I have inherited a project that uses the following pattern for passing parameters from the code behind to the aspx page. I know this is wrong, but I am on the fence on the best way to refactor it.

Code Behind:

using System;
using System.Web;

namespace BadPassing
{
    public partial class _Default : System.Web.UI.Page
    {
        private Random rnd = new Random(); //Is this bad?

        protected int numberOne; //this is bad
        protected int numberTwo; //this is bad

        protected void Page_Load(object sender, EventArgs e)
        {
            numberOne = rnd.Next(100);
            numberTwo = rnd.Next(100);
        }
    }
}

ASPX Page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BadPassing._Default" %>

<!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" >
<head runat="server">
    <title>Bad Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <a href="http://www.google.com/search?q=&lt;%=this.numberOne %>%2B<%=this.numberTwo %>">
            Add <%=this.numberOne %> and <%=this.numberTwo %> using google.
        </a>
    </div>
    </form>
</body>
</html>

My understanding is that the numberOne and numberTwo are not thread-safe, and could cause incorrect behavior if two people loaded the page at the same time. Furthermore, if the page relied on numberOne and numberTwo to store values between postbacks, multiple simultaneous users would cause unexpected results.

Is my understanding of why this the above technique is so bad correct, and if so, how would you best refactor this code?

On a side note, is it incorrect to store stateless page-level services (like Random) as member variables of the page class?

+1  A: 

In the normal case if two users load the page at the same time they will be using different instances of the page. As numberOne and numberTwo are instance variables there will be no shared state and no threading problem.

Brownie
+3  A: 

This code is ok. The member variables are instantiated every time the page loads, as a new instance of a class is created on every page load. If the variables were static, only then would you have problems with 2 people loading the page at the same moment.

Think about the controls you drop on a page. They are member variables, but they don't have problem with the page being loaded at the same time by different requests.

Paul Whitehurst
Thank you. I completely misunderstood the ASPX render cycle. This clarifies much.
Ryan Michela
A: 

Whilst there isnt really anyhting wrong with the code (as already mentioned) if you wanted to refactor it, I'd probably do something along the lines of

using System; using System.Web;

namespace BadPassing
{
    public partial class _Default : System.Web.UI.Page
    {
        private Random rnd = new Random(); //Is this bad?

        protected int numberOne; //this is bad
        protected int numberTwo; //this is bad

        protected void Page_Load(object sender, EventArgs e)
        {
            numberOne = rnd.Next(100);
            numberTwo = rnd.Next(100);
            searchLink.NavigationUrl = String.Format("http://www.google.com/search?q={0}%2B{1}", numberOne, numberTwo);
            searchLink.Text = String.Format("Add {0} and {1} using google", numberOne, numberTwo);
        }
    }
}

and then on your aspx page just add an <asp:hyperlink> control

lomaxx