是否有可能突破code-背后成多个部​​分的文件? [英] Is it possible to break code-behind into multiple partial files?

查看:112
本文介绍了是否有可能突破code-背后成多个部​​分的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.aspx.cs code-后面的.aspx Web窗体。在code-背后是近2000线长,而且越来越的地步,只有这样,才能轻松地浏览它是把一吨的空间在部分之间,缩小这样我就可以看到的物理外观code,然后放大,我想编辑。换句话说,这是一个重大的痛苦。我想这个2000线分割为重present概念在code不同的文件。所以,当我需要去编辑页面的雇员选项卡的jQuery的一些功能,我可以去到只包含雇员的功能部分类。

I have a .aspx Web Form with a .aspx.cs code-behind. The code-behind is nearly 2000 lines long, and it's getting to the point where the only way to easily navigate it is to put a ton of spaces in between the sections, zoom out so I can see the physical look of the code, and then zoom in where I want to edit. In other words, it's a major pain. I'd like to split this 2000 line into different files that represent concepts in the code. So when I need to go edit some functionality in the "Employee" jquery tab of the page, I can just go to the partial class that contains just the "Employee" functionality.

在新建项目菜单,我无法找到一个额外的code-隐藏文件,没有文件.aspx.cs什么。我试图创建一个cs文件,它重命名为.aspx.cs,并给它相同的部分类名---不走,我看不出任何的默认部分类的方法,也没有对控件页。

In the New Item menu, I cannot find anything for an additional code-behind file, no .aspx.cs file. I tried creating a .cs file, renaming it to .aspx.cs, and giving it the same partial class name --- no go, I couldn't see any of the methods of the default partial class, nor the controls on the page.

我理解这个概念,如果你的阶级,在OOP意义上说,是长,它做太多。我不明白的是在Web表单code-背后的条款阶级。该表格​​不能真正被分解成更小的形式---用户体验需要的所有发生在一个页面上。

I understand the concept that if your "class", in the OOP sense, is that long, it's doing too much. What I don't understand is a "class" in terms of code-behind for a web form. The form can't really be broken down into smaller forms --- the user experience needs to all take place on one page.

推荐答案

您可以使用部分类。因此,一个新的的.cs 文件添加到您的项目并使用部分类。

You could use partial classes. So add a new .cs file to your project and use a partial class.

例如假设您目前有如下的 Default.aspx.cs 文件:

For example assuming you currently have the following Default.aspx.cs file:

public partial class _Default : System.Web.UI.Page
{
    ... some 2000 lines of code
}

您可以添加另一个.cs文件到您的项目中:

you could add another .cs file to your project in which:

public partial class _Default
{
    ... a subset of the 2000 lines of code could be moved here
        in order to isolate some functionality
}

这是非常重要的,这部分的类是完全相同的命名空间与原始类中声明并具有相同的名称。

It's very important that this partial class is declared in exactly the same namespace as the original class and has the same name.

但需要注意的是使用部分类隐藏的仅仅是狗屎。它不干净。如果要清理它,你将不得不考虑OOP的做法不同类别拆分功能。你应该分层应用程序。

But note that using partial classes only hides the shit. It doesn't clean it. If you want to clean it you will have to consider OOP practices to split functionality in different classes. You should layer your application.

这篇关于是否有可能突破code-背后成多个部​​分的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆