简体   繁体   中英

WinForm App Data Persistance (C#)

I beleive the best ways of "variable short term" persistance in an ASP.NET application are:

  1. Sessions Variable (Session Scope)
  2. Application Variable (Application Scope)
  3. Page View (Page Scope)
  4. Application Settings (Application Scope)
  5. ???

What are the best ways of "variable short term" persistance in a windows form application for:

  1. Form Scope
  2. User Session Scope
  3. Application Global Scope

Thanks

Well, for "Form Scope" you can simply use fields or properties. For application settings and session settings you can use a (static) class, or anything else that is convenient.

Note that there really is no difference between Application and Session in a WinForms app, you're not on a Server anymore.

Right-click the project, select properties->Settings. You can edit persistent fields (ie settings), specifying name, type and scope (user-wide or application-wide).

You can access them from the code by <Default Namespace>.Properties.Settings.Default.

The settings are persistent between application runs.

You should use these settings for the Form Scope too.

All these settings make sense for storing persistent values between application runs. Use regular (static) fields for storing data within one program instance.

You can specify whether a settings is for the current user or global when you create it. If you look in the projects properties in VS you see this

alt text http://img268.imageshack.us/img268/9186/projectsettings.png

For variables only accessible by the form I would just make them private fields. There isn't such as a thing as a "session" in a win forms application, however, you could use the CallContext to simulate a session as HttpContext and Session in a Web application are based around this class.

Anything global I would probably store in the Application object itself or the application configuration file.

I'm not absolutely sure what you are looking to persist beyond the boundaries of the object lifecycle but as Henk has stated, your Form has scope for the duration of time that it's loaded and you can add properties to the form that can be initialised by your code when the Form is instantiated and will last till the Form is unloaded. The next scope up is really the Application object (unless you wrap the forms in some kind of custom container class) where you could add properties for the life of the application (and essentially the Application object).

To persist beyond the scope of the Application then use the Properties class or store data in the registry (in an appropriately identified and named location).

It sounds like you are thinking somewhat procedurally and it sounds a bit like global variable (or at least larger than method or object scope) persistence. Rather than thinking in terms of variables, think in terms of objects and properties of those objects. If you've correctly designed your object model then the persistence of the appropriate properties should be a function of that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM