As I usually do not have much patience and persistence in writing a blog, I will not be able to finish it after three minutes, so although it is relatively late (just after ), but for the final formation of this blog, I decided to catch up with it overnight.
Recently, I have found a new wave of discussion on the blog, especially about WebForm and MVC. Such a discussion can promote everyone's progress and give everyone a deeper understanding of each technology. Different technologies have their own best practices, such as WebForm and MVC. If we all follow the basic usage principles of each technology in common development practices, rather than abuse it, then I don't believe it will get the best effect.
Here, I want to discuss how to use performancecontrol reasonably to simplify some data operations on the page, replace some of the parameter processing work, and finally replace the background code file on the page. This idea comes from an article in asp.net Blogs (I can't find the link to this article, which means that if I use performancecontrol to encapsulate some complex data processing) based on NBearDataSource and NBearDataSource, a website project has been developed and is considered as a recommended WebForm development method.
In ASP. in. NET 2.0, a new data binding method is proposed, that is, the performancecontrol control is used to query data, and the data control itself can be associated with the performancecontrol by specifying the performanceid, the performancecontrol itself will automatically specify and query parameters based on different implementations. In ASP. NET 2.0, ObjectDataSource, SqlDataSource, and AccessDataSource are provided. It can be said that we often use ObjectDataSource, but it is still complicated in many cases due to functional limitations. At this time, if only the performancecontrol provided by the system is used, its functions will be basically abolished.
My practice of DataSouceControl is to encapsulate different modules (or data queries) into one performancecontrol. The base class of performancecontrol can be ObjectDataSource or inherited directly from System. UI. performancecontrol and implemented on the basis of abstract methods. However, we recommend that you still use a DataSourceControl base class to process public logic. For example, if you use the NBear solution, you can inherit from NBearDataSource; or if you use LINQ, you can create a LINQDataSource. The following uses NBearDataSource as an example to describe the benefits:
1) In NBearDataSource, for data query, we only need to specify the object type, then generate WhereClip Based on the condition parameter, and then call the NBearDataSource. Filter Method to complete a simple object query. You do not need to call the Gateway query interface.
2) In NBearDataSource, the data transferred during data submission (new and modified) is assigned to the corresponding object. For example, if you use FormView and DataSourceControl to add and modify data, you do not need to obtain the fields submitted in FormView one by one from the input control and Bind them in two directions, you can obtain all input field values in performancecontrol. NBearDataSource assigns all submitted fields to a specified object, which can be saved to the database.
3) Each performancecontrol can be flexibly controlled based on its own functions. And serves as a bridge between pages and logical services.
In this practice, performancecontrol is the core of the entire data presentation and a bridge. It may be equivalent to C in MVC. However, this application in website projects has the following advantages:
1) It can work closely with any data binding controls to query and bind data at the most appropriate time without user intervention. This prevents re-binding in many cases.
2) The use of performancecontrol allows the background to write a lot of code for data binding, including paging code, all retired.
3) using performancecontrol makes it easy to reuse a part of data on different pages without providing complex query conditions and calling the required query interface.
4) performancecontrol can encapsulate the transmission of query parameters. For example, if your query parameter comes from the URL, you can submit the parameter processing to performancecontrol.
5) performancecontrol is advantageous in automatic field ing when data is submitted.
6) performancecontrol is very flexible. If we do not need to change the appearance of the page, we only need to modify the performancecontrol in the data source.
After doing so, the project may have a lot of performancecontrol, but at the same time we can reduce the page background code, so that occasional server code can be written in the aspx file, making the page easier to maintain, it takes effect without compilation. At the same time, if you put performancecontrol in an independent Library, it will be easier to reuse it.
Similarly, this mode is not available everywhere. Only applicable to data binding controls of WebForm. (I will not talk about the performance of data-bound controls. Lao Zhao has clearly explained that the performance of HTML generation by repeatedly binding controls to data is no problem, especially its componentization idea is very good .)
Finally, a simple reuse method is provided. For example, there are many enumeration types in your project. to display these enumeration types on the page, you may need some code. At this time, we can write an EnumDataSource, as long as an enumeration type is specified for EnumDataSource on the page, the EnumDataSource reads the name, value, and Chinese description of the enumeration. For some enumerations, EnumDataSource is used for binding. Whether it is DropDownList or CheckBoxList, do you think it is very simple?