ASP.NET MVC: TempData Keep vs Peek Methods

TempData Keep vs Peek Methods In Asp.Net MVCIntroduction: In this article you will learn the following:

1) What is TempData?
2) How it is used to persist data for next request?
3) Use of Keep and Peek methods to persist data. 
4) Similarities and differences between Keep and Peek methods. 


Description: TempData helps to maintain data when we want to transfer data from one action method to another action method of the same or a different controller as well as redirects.

The answer of above three questions out of four is already given in one of my previous article Using Keep and Peek with TempData to Persist data
I suggest to read that article first to get better understanding of TempData and its Keep & Peek methods.

Below are some of the similarities and differences between Keep and Peek methods of TempData

Similarities between Keep and Peek methods.

Both Keep and Peek methods are used to persist data for next request i.e. data that automatically destroys after read will be persisted for next request.

Differences between Keep and Peek methods.

Keep Method:

1) MSDN Says Keep() method marks the specified key in the dictionary for retention.

2) To get and persist TempData value using Keep method two requests are required: first to read the value and then in next statement we call Keep method to persist value to ensure it is available to next request.

3) TempData.Keep() have 2 overloaded methods.

void keep() : it persists all TempData Objects and prevents them to delete on current request completion to ensure they are available for next request.

void keep(string key) : It persists specific item in TempData with the help of name.

4) Both RedirectResult  and RedirectToRouteResult internally calls Keep method to persist items

5) Once we retrieved value from TempData object then it is marked for deletion, with Keep method we can later prevent object from Deletion. It means using Keep method after reading the TempData, first deletion marking is done then persisting value is done.

6) Keep method should be used when persisting the value depends on additional logic.

Peek Method:

1) MSDN Says Peek () method returns an object that contains the element that is associated with the specified key, without marking the key for deletion.

2) Using Peek method both the operation of accessing and persisting value is done in a single Call/statement.

3) No overloaded methods available in case of Peek method. It is meant to save particular TempData's value.

4) Peek method is not called internally with any of ActionResult.

5) With Peek method we can read and persist TempData value without marking for deletion in just a single call/statement. It means deletion marking is not happening in case of Peek method. It directly persist TempData.

6) Peek should be used when we always want to preserve the value for another request

 Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.  
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..