ASP.Net C# - 将代码从Codebehind转移到类文件

时间:2010-07-17 00:42:13

标签: c# asp.net encapsulation

一段时间以来,我试图找出如何重构我的一些代码以减少整个应用程序的冗余。我只是学习OOP的基础知识,可以创建简单的类和方法,但我的知识在实际适用性方面是有限的。以下一些代码说明了我的沮丧:

#region DELETE selected users - button

protected void btnDeleteSelected_Click(object sender, EventArgs e)
{
    try
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("chkRows");
            if (cb != null && cb.Checked)
            {
                // get the row index values (DataKeyNames) and assign them to variable
                string userName = GridView1.DataKeys[row.RowIndex].Value.ToString();

                // delete selected users and their profiles
                ProfileManager.DeleteProfile(userName);
                Membership.DeleteUser(userName);

                Msg.Text = "User(s) were sucessfully <b>DELETED</b>!";
                Msg.Visible = true;
            }
        }
    }
    catch (Exception ex)
    {
        Msg.Text = "Oops! " + ex.Message;
        Msg.Visible = true;
    }
    finally
    {
        // refresh gridview to reflect changes
        GridView1.DataBind();
    }
}

#endregion

在代码隐藏文件页面中,我的项目的几个页面上使用了这段代码。如何将其移动到类文件中。我不知道如何在类中引用类似gridview的对象,因为它不像在实际页面上那样存在。

有人可以帮帮忙吗?谢谢。

2 个答案:

答案 0 :(得分:2)

在尝试重构代码时,通常会应用许多原则。目前,您正在尝试重构您的代码,以免发挥DRY原则(DRY =不要重复自己)。重构该代码将是一个很好的举动。

但是,其他一些校长可能会参与其中你可能想要考虑的事情。 single responsibility principle表明每种方法只能做一个明确的事情。考虑一下您当前方法的操作。它从GridView中提取用户名,然后删除与该用户关联的一些数据。作为两种方法,这可能会更好。

此外,loosely coupled代码也很好。您不希望类之间存在大量依赖关系。例如,如果将整个方法移动到单独的类或库中,则该类或库将依赖于ASP.NET库,因为它对GridView控件进行了特定引用。但它不需要。您可以使用一个单独的方法将用户名从GridView中拉出(此方法与ASP.NET紧密耦合),然后单独执行其余操作(例如删除)用户的数据),只需要用户名。第二种方法不会以任何方式与ASP.NET耦合。因此,通过使用各自具有单一责任的单独方法,您将拥有更松散耦合的代码。二合一。

至于你在这里说的话:

  

我不知道如何引用   对象就像一个类中的gridview   因为它不像它那样存在   在实际页面上。

当您调用提取用户名的方法时,您只想传递对GridView的引用。像这样:

public static class Util
{
    public static IEnumerable<string> GetUsernames(GridView gv)
    {
        List<string> userNames = new List<string>();
        foreach (GridViewRow row in gv.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("chkRows");
            if (cb != null && cb.Checked)
            {
                // get the row index values (DataKeyNames) and assign them to variable
                string userName = gv.DataKeys[row.RowIndex].Value.ToString();
                userNames.Add(userName);
            }
        }
        return userNames;
    }
}

现在,在你的任何asp.net代码页面后面,你可以这样做:

IEnumerable<string> usernames = Util.GetUsernames(GridView1);
foreach(string username in usernames)
    doSomething(username);

doSomething(username)将调用您执行删除操作或其他任何操作的其他方法。

希望这有帮助。

哦,如果你只是学习OOP的基础知识,我会推荐像Head First Object-Oriented Analysis and Design这样的东西,或任何似乎足以涵盖这个主题的书。我喜欢O'Reilly Head First系列。这些信息非常容易消化。

答案 1 :(得分:1)

在Webforms应用程序中,将代码中的应用程序和/或业务逻辑分解出来的常用技术是使用MVP模式。这并不意味着通常在代码隐藏中找到的所有代码都会被移动到另一个类,因为这与代码隐藏没有任何不同。 UI呈现逻辑(例如,数据绑定设置,对UI控件的访问等)最好留给代码隐藏,但业务逻辑(在这种情况下,删除用户)由Presenter中的方法执行。 / p>

在您的情况下,我将构建一个用户名集合并在Presenter上调用RemoveUsers()方法,传入用户名列表,该列表将处理与ProfileManager和Membership组件的接口(理想情况下通过抽象)。这允许您为Presenter中的逻辑编写单元测试。