DD Class4

esse quam videri
Jump to: navigation, search


Into To State

In .NET there are 4 places to persist objects without using external data stores.

  • Viewstate - persists state on a single page
  • Session - persists state for a unique visitor
  • Application - persists state for an application
  • Cache - persists data on a page or site with dependencies based on policy

Application State

Application - application level variables have application scope meaning they are available to the entire application. The application object is free threaded so it must be locked and unlocked to ensure data intergrity


Application.Lock();
    Application["Test"] = s;
    Application["Dog"] = new Dog { Name = "TestDog", Age = 7 };
    Application.UnLock();

http://msdn.microsoft.com/en-us/library/ms178594(v=vs.100).aspx


example

http://iam.colum.edu/dd/classsource/application/ApplicationSet.aspx

ApplicationSet.aspx - source

http://iam.colum.edu/dd/classsource/application/ApplicationSetUpdateFixed.aspx

ApplicationSetUpdateFixed.aspx - source

Global asax

The file global.asax allow us to overload application state events. The is a good place to set application constants make global changes to application state and session state.

global.asax-source

http://iam.colum.edu/dd/classSource/application/ApplicationCnt.aspx - source

Dog and Dog Binding

DataBound controls can bind to anyting that implements IEnumerable It's easier in 2.0 + to just inherit from ICollection

http://iam.colum.edu/DD/classsource/binding/DogBinding.aspx source codebehind

Session State

Session is an array of objects that is help on the server for each unique user. Each time a browser requests an aspx page the sever sends back a unique id as a cookie called SESSIONID. This cookie is used to retrieve each browsers session array on each request.

Boxing and unboxing of session variables

All Session variables are of type object. This means that you can put whatever you like into the session array. The trouble starts when you remove and object from the array it will always be removed and be of type object. You then need to cast the object into its actual data type. This is known as boxing and unboxing.

Session["Counter"] = 1;  //set the session variable Counter equal to the integer 1 the int is boxed as an object

int i = (int)Session["Counter"] //cast the object Session["Counter"] bact to an int unboxing

exmaple with boxing and unboxing of ints

http://iam.colum.edu/dd/classsource/SessionCnt.aspx SessionCnt.aspx - source

Dog examples

The first page creates a dog objects and stores it as a session variable.

http://iam.colum.edu/dd/classsource/dogsession/dog.aspx dog.aspx - source

http://iam.colum.edu/dd/classsource/dogsession/dog_session_Page1.aspx dog_session_Page1.aspx - source

http://iam.colum.edu/oop/classsource/dd/dogsession/dog_session_Page2.aspx dog_session_Page2.aspx - source

http://iam.colum.edu/dd/classsource/dogsession/dog_session_Page3.aspx dog_session_Page3.aspx - source

View State

ViewState is an instance of statebag todo demostate viewstate

look at guessing game

http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class10/assign3

Cache

The built in output caching ins useful for speeding up pages that don't need to be re rendered on every hit. The caching mechanism is very flexible and powerful. It can cache multiple versions of a page based on policy or content interactions. Page caching can make you website many many times faster.

Output Cache

QuickStarts OutputCache

http://iam.colum.edu/dd/classsource/State/cache/Cache.aspx - source http://iam.colum.edu/dd/gbrowser.php?file=/classsource/State/cache/Cache.aspx.cs

http://iam.colum.edu/dd/classsource/State/cache/CacheByControl.aspx - source http://iam.colum.edu/dd/gbrowser.php?file=/classsource/State/cache/CacheByControl.aspx.cs

More Event Samples

A nice example of a web page that uses events to create and use our dog class. Notice hot the events and the dog class are only loosely coupled (the doesn't directly use the dog class).

http://iam.colum.edu/oop/classsource/class10/events/DogDelegateEvent.aspx DogDelegateEvent.aspx - source

http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class10/events

Home Work

Create a simple web guessing game similar to http://iam.colum.edu/dd/classsource/State/Guess.aspx

The game should use WebForms and Events and use either ViewState, Sessions, Applications to store data.