Best practices for storing data on an ASP.NET web page

I have a multi-user set of ASP.NET web pages. The pages use AJAX update panels, so I can avoid updating the screen with every postback. The life cycle of each page is as follows:
1. During Page_Load, get relevant data for the user from the web service.
2. Save the data (rather large) and the service link in a static dataset.
3. Allow various changes to data parts using screen controls (grids, text fields)
4. Verification of data received through the form
5. send updated data back to the service

I do this using static variables in the page class itself as follows:

public partial class MyPage : System.Web.UI.Page { static xxxx.DataCaptureServiceClient m_Service; //reference to web service static string m_PersonID = string.Empty; //current person_id page is viewing static ServResponse m_ServiceResult = null; // reference to our data to edit ( ServResponse is a large data contract) static string m_SortExpression = "Reference"; //default sort expression for grid const int PERSONID_COLUMN = 0; //column index in grid for the personID column const int STATUS_COLUMN = 4; //column index in grid for the application status protected void Page_Load(object sender, EventArgs e) { try { if (!Page.IsPostBack) { //get new service instance m_Service = new xxxx.DataCaptureServiceClient(); ShowDataOnPage(); //get data in m_ServiceResult and bind to a grid on screen } } catch (Exception ex) { Response.Redirect("ErrorPage.aspx", false); } } protected void butNext_Click(object sender, EventArgs e) { try { Page.Validate(); if (Page.IsValid) { //use m_ServiceResult and m_Service to send a packaged submission to the service SendDatatoService(); Response.Redirect("TheNextPage.aspx", false); } } catch (Exception ex) { Response.Redirect("ErrorPage.aspx", false); } } //Other methods which allow edits to m_ServiceResult 

I am wondering if:

A) Is this a good way to implement or are there best practices?
B) Should I clear the memory by setting all statics to NULL when I redirect to another page?
C) If I delete statics, do I risk losing data from another user?

UPDATE

I rewrote removing statics, storing const values โ€‹โ€‹and passing me the data as parameters. Where I need to store data for updates, I saved the minimum amount that I need in the session [] variables.

+4
source share
4 answers

A) No - what happens if the second user opens the page and the other is busy? Will the static data set be overwritten with 2 user data or will your static data set somehow differentiate data from different users at the same time?

B) If you absolutely must use statistics / server-side data, then yes, you must somehow clear them. However, ensuring that this happens is difficult. (For example, if one user just closes his browser)

C) Itโ€™s possible, but if it causes concern, then my question in A) will already cause big problems for you.

As a general answer, storing large amounts of data in memory on a server is generally a bad practice. It does not scale very well and reveals many different types of errors. Your back-end must be inactive, and you can achieve this in several ways, for example, by storing records in a separate table in the database that are only being finalized (and thus are moved to the โ€œrealโ€ tables) to the end of several screens that you have there is.

+2
source

In a direct answer to your questions without opening a can of worms

A) There is not so bad as implementing data capture
B) Setting variables to null in .NET does not clear memory.
C Yes, yes By definition, each user uses the same static data.

+1
source

I would save your service decans locally and use only global variables for constants. You will not save much by declaring them globally. Also, I would use the string const instead of the static string.

+1
source

I think others answered your questions. My suggestion to help fix your code will be as follows:

  • m_PersonID does not have to be static - save it as a property / field of the page instance and set it as static. In fact, the class-level variables in the code behind can very quickly succumb to fairly unread code. Does it need to be at the class level or can it be defined in the method in which you need it (and maybe just passed as an argument to other methods)?
  • m_ServiceResult is the same for this. Not sure why you have it, and it doesn't have to be static. Try moving it to a method level variable.
  • m_SortExpression may just be const
  • m_Service - again, I don't see the need for this to be static. If this is just a proxy for the service, I donโ€™t think you need to keep it in memory to avoid unnecessary overhead. I think that the problems associated with its statics far outweigh the small overhead associated with the fact that the service client is a method level variable.
  • Regarding a static dataset, you might consider caching a large set of results if the data for the user is pretty static. Or, really take a look at what you are returning and see if you really need all this data. If you do calculations in code, this is probably not the best place for it. Consider your service level as the place for this computation (or db if you have a lot of your logic in stored procedures - not to mention the place for it, but just recognizing that this is an opportunity).
  • In addition, the data set is quite large and bulky. Do you need this structure or a set of ordered objects of objects is more appropriate? This item is pretty much related to # 5, because you really need to evaluate how you retrieve data, save it, and act on it. A dataset can be a quick and dirty answer, but usually it is not an effective answer.

In short, dump the statics and move on to private method level variables where possible. Try to reduce the amount of data that you return from the database. Consider methods that take parameters instead of using class variables and / or global variables.

Hope this helps. Good luck

+1
source

All Articles