在ASP.NET中,将大型代码放在文件后面

时间:2021-07-09 15:28:39

In theory a large 1000+ line of code behind code is "bad practice" what if the majority of the code effects controls on the page?

在理论上,代码后面的1000多行代码是“坏习惯”,如果大部分代码效果控制在页面上怎么办?

For example what if there were 30 text boxes to collect user data, but depending upon answers to questions, visibility, validation, etc changed on these text boxes.

例如,如果有30个文本框来收集用户数据,但是取决于对问题的回答、可见性、验证等的更改,该怎么办?

Should you then be writting methods in classes for validation that take collections of text boxes and disable validation, or set the visibility? I'm having a hard time wrapping my mind around the design practices of large code files.

然后,您应该在类中编写方法以进行验证,以获取文本框集合并禁用验证,还是设置可见性?我很难把我的思想集中在大型代码文件的设计实践上。

I guess I'd like to know the best practice for breaking out large code behind files that still allows for easy control manipulation.

我想我想知道在文件背后破解大型代码的最佳实践,这些文件仍然允许简单的控制操作。

2 个解决方案

#1


2  

I'd devide the text boxes into groups depending on the contents. Make a User Control for each group (With a dedicated visual container). A user control can take care of validation and some computations, but it is very readable and can event be reused somewhere else if the problem you are solving allows that.

我会根据内容将文本框分成组。为每个组创建一个用户控件(使用专用的可视容器)。用户控件可以处理验证和一些计算,但是它是非常可读的,如果您正在解决的问题允许的话,可以在其他地方重用事件。

Another option is to use a wizard. This could be more complicated, but if you have the time, you can get nice results.

另一个选择是使用向导。这可能更复杂,但是如果你有时间,你可以得到很好的结果。

#2


0  

First of all I do not think 1000+ is not too much for a code behind file at all and I wouldn't consider this bad practice as long as you follow the DRY ( do not repeat yourself ) approach. If you really want to split this up there might be a possibility to have more code behind files for group of controls and do ajax requests to validate parts of the page. You could then evaluate the results from the ajax calls and check if the return message is success and assume that the validation is successfully if all individual ajax calls have a success flag.

首先,我不认为1000+对于文件后面的代码不太合适,只要遵循DRY(不要重复自己)方法,我就不会认为这种做法不好。如果您真的想将其拆分,那么可能会有更多的代码隐藏在文件后面以进行组的控制,并执行ajax请求来验证页面的某些部分。然后,您可以评估ajax调用的结果,并检查返回消息是否成功,并假设如果所有单独的ajax调用都有一个成功标志,验证就成功了。

For ASP.NET I do most of the logic on the client side with the knockout framework using the visible binding: http://knockoutjs.com/documentation/visible-binding.html

ASP。NET中,我在客户端使用knockout框架执行大部分逻辑,使用的是可见绑定:http://knockoutjs.com/documentation/visiblebinding.html

I use knockout to generate the controls via foreach binding and set the visible binding to an observable inside the obserable array. If the array you pass to the foreach binding is an obserable array you can also add / remove controls via JS.

我使用knockout通过foreach绑定生成控件,并将可见绑定设置为可观察数组中的可见绑定。如果您传递给foreach绑定的数组是一个可观察的数组,您还可以通过JS添加/删除控件。

You could use a validation framework and on validation you can change the observable array adding more questions / removing questions from HTTP post.

您可以使用一个验证框架,在验证时,您可以更改可观察数组,添加更多的问题/从HTTP post中删除问题。

Another option would be to do the validation on the server side. You can check in the post method the values which have been submitted and change the view model adding or removing elements. You then return the view with the modified model.

另一种选择是在服务器端进行验证。您可以在post方法中检查已提交的值,并更改视图模型添加或删除元素。然后返回带有修改后的模型的视图。

If required I would use a strongly typed collection to have a reference for the page elements and modify the properties as required.

如果需要,我将使用强类型集合来引用页面元素并根据需要修改属性。

For WPF I use Observable collections to track property change events which notifies the UI when the collection changes.

对于WPF,我使用可观察集合跟踪属性更改事件,当集合更改时,这些事件将通知UI。

#1


2  

I'd devide the text boxes into groups depending on the contents. Make a User Control for each group (With a dedicated visual container). A user control can take care of validation and some computations, but it is very readable and can event be reused somewhere else if the problem you are solving allows that.

我会根据内容将文本框分成组。为每个组创建一个用户控件(使用专用的可视容器)。用户控件可以处理验证和一些计算,但是它是非常可读的,如果您正在解决的问题允许的话,可以在其他地方重用事件。

Another option is to use a wizard. This could be more complicated, but if you have the time, you can get nice results.

另一个选择是使用向导。这可能更复杂,但是如果你有时间,你可以得到很好的结果。

#2


0  

First of all I do not think 1000+ is not too much for a code behind file at all and I wouldn't consider this bad practice as long as you follow the DRY ( do not repeat yourself ) approach. If you really want to split this up there might be a possibility to have more code behind files for group of controls and do ajax requests to validate parts of the page. You could then evaluate the results from the ajax calls and check if the return message is success and assume that the validation is successfully if all individual ajax calls have a success flag.

首先,我不认为1000+对于文件后面的代码不太合适,只要遵循DRY(不要重复自己)方法,我就不会认为这种做法不好。如果您真的想将其拆分,那么可能会有更多的代码隐藏在文件后面以进行组的控制,并执行ajax请求来验证页面的某些部分。然后,您可以评估ajax调用的结果,并检查返回消息是否成功,并假设如果所有单独的ajax调用都有一个成功标志,验证就成功了。

For ASP.NET I do most of the logic on the client side with the knockout framework using the visible binding: http://knockoutjs.com/documentation/visible-binding.html

ASP。NET中,我在客户端使用knockout框架执行大部分逻辑,使用的是可见绑定:http://knockoutjs.com/documentation/visiblebinding.html

I use knockout to generate the controls via foreach binding and set the visible binding to an observable inside the obserable array. If the array you pass to the foreach binding is an obserable array you can also add / remove controls via JS.

我使用knockout通过foreach绑定生成控件,并将可见绑定设置为可观察数组中的可见绑定。如果您传递给foreach绑定的数组是一个可观察的数组,您还可以通过JS添加/删除控件。

You could use a validation framework and on validation you can change the observable array adding more questions / removing questions from HTTP post.

您可以使用一个验证框架,在验证时,您可以更改可观察数组,添加更多的问题/从HTTP post中删除问题。

Another option would be to do the validation on the server side. You can check in the post method the values which have been submitted and change the view model adding or removing elements. You then return the view with the modified model.

另一种选择是在服务器端进行验证。您可以在post方法中检查已提交的值,并更改视图模型添加或删除元素。然后返回带有修改后的模型的视图。

If required I would use a strongly typed collection to have a reference for the page elements and modify the properties as required.

如果需要,我将使用强类型集合来引用页面元素并根据需要修改属性。

For WPF I use Observable collections to track property change events which notifies the UI when the collection changes.

对于WPF,我使用可观察集合跟踪属性更改事件,当集合更改时,这些事件将通知UI。