如何在CS后面拆分(使用Partial类)代码? [英] How to Split(using Partial class ) Code behid .CS?

查看:83
本文介绍了如何在CS后面拆分(使用Partial类)代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是我的代码.文件后面包含五千行,现在我想将代码分成两个文件...
另一个文件也接受Web控件事件

我尝试了部分课程

这是我在文件后面的代码

I requirement is my code Behind file contains more than 5 thousand line, Now i want to split the code into two file ...
The other file also accept the web controls events

i tried with partial class

This is my Code Behind File

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}




这是我的课程添加到App_code文件夹中




this my class added to App_code Folder

public partial class _Default : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
    }
}



这里的Button1_Click方法是Web控件.



here the Button1_Click method is web control

推荐答案

图层是您需要的图层.但是,如果您不知道如何在表示层和业务层中分离代码,则可以使用分层页面.

例如

这是应用程序代码中的类文件.将所有通用代码放在这里.我显示的内容是错误的,将事件处理程序放在此处并不是一个好建议.常见的业务逻辑可以放在这里.我只是在这里放了一个事件处理程序,目的只是为了展示这种可能性.

Layers is the one you needed. But if you don''t know how to separate you code in the presentation and business layers then in your case you can use hierarchical pages.

For example

this is a class file in the app code. Put all the common codes here. what I shown is wrong, it is not good advice to put event handler here. Common business logic can go here. I just put an event handler here just to show the possibility.

public partial class BasePage : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("button Clicked");
    }
}



现在,您可以从此类继承页面.



Now you can inherit your page from this class.

public partial class _Default : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click +=new EventHandler(Button1_Click);
    }
}




现在,单击按钮位于另一个文件中,但可以从您的页面代码中调用该按钮.您可以将通用函数移至父类并相应地继承页面,而不是使用此类事件处理程序.




Now the button click is in another file, but can be called from your page code. Instead of using event handlers like this, you can move the common function to a parent class and inherit the pages accordingly.


将类拆分为部分声明和将它们放在不同的部分.

我只想知道即使拆分的类也不应该太大,所以可以拆分许多功能层,不仅像同一类的部分一样,还可以抽象为不同的类.

这是一个重要功能,此处未作说明,但是对于可支持性来说绝对是很棒的.
您不需要为局部的每个部分重复所有基类/接口的列表!实际上,该列表是合并了部分声明的所有部分.这是一个非常好的功能.

为什么这很重要.例如,考虑一个表单.您只有一个基类声明:如partial class MyForm : Form {/*...*/}.如果从正确的模板开始,会为您生成两个局部,然后使用局部声明添加另外的1,2,.. N个部分.非常重要的一点是,不要在所有这些文件中都重复": Form".为什么?因为这样,您可以通过在一个文件中仅更改一个单词来更改基类.这是实现Form继承的简单途径,否则很容易修改继承.

界面带来了更多令人印象深刻的好处.假设您要实现多个接口.在不同的文件中执行此操作.由于您不必在列表中的:"之后重复所有接口,因此只需在文件中提及每个接口的实现位置即可.好处是可以将关注点,代码的可读性和可支持性很好地视觉隔离.

—SA
This is absolutely correct and good idea to split a class into partial declarations and put them in different parts.

I only want to know that even split class should not too big, so many functional layers could be split not just like partials of the same class, but abstracted as different classes.

This is one important feature which was not clarified here, but is absolutely great for supportability.
You don''t need to repeat the list of all base classes/interfaces for each part of the partial! Effectively, the list is merged from all parts of partial declarations. This is a very good feature.

Why this is very important. Consider a Form, for example. You have just one declaration of the base class: like partial class MyForm : Form {/*...*/}. Two partials are generated for you if you start with the proper template, and then you add 1,2,.. N more parts using partial declaration. It is very important of you do not repeat ": Form" in all those files. Why? Because this way, you can change the base class by changing just one word in one file. This is an easy pathway to Form inheritance and otherwise good to modify inheritance.

Even more impressive benefits are brought to interfaces. Imagine you want to implement several interfaces. Do it in different files. As you don''t have to repeat all interfaces in the list after ":", you only mention each interface in the file where it is implemented. The benefit is good visual separation of concerns, readability of code and supportability.

—SA


这篇关于如何在CS后面拆分(使用Partial类)代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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