Clearer MVC

In every application there are some variables that are set by the ASP.NET  application(  ASP.NET_SessionId  cookie ) and some that are set by the programmer( cached data in Application/Session/Cache/Cookies and so on).

I wanted every time to have a page where I can “clear” / delete those – and not found. So it’s the Clearer project.

It consists of :

  1. ClearerController with 2 Actions:   Index and DeleteItem
  2. 2 Views : Index.cshtml and EditAppData.cshtml
  3. Different Models:
    • SourceData  – enum  – can be  :  None,Application,Cache,Session,Cookies
    • AppData –  maintains Key/Value and SourceData pairs
    • ListAppData – loads data from Application,Cache,Session,Cookies  – and deletes.

To make an example,I have put in the Application_Start and Session_Start different values. So the screen is the following:

image

What I learn from the code:

  1. The Cookies,Applications,Session,Cache items can be easily converted to an DictionaryEntry and the code can be like this:
     DictionaryEntry de = new DictionaryEntry(item,sess[item.ToString()]);
     AddNew(de,SourceData.Session);
    
  2. Code must be error prone – what if some item in Session is null ? So,if I have the Key,all is good:
    private void AddNew(DictionaryEntry de,SourceData sd)
            {
                AppData ap = new AppData() { source = sd,Key = de.Key.ToString() };
                try
                {
                    var obj = de.Value;
                    ap.Value = (obj == null) ? Null : obj.ToString();
                }
                catch (Exception ex)
                {
    
                    ap.Value = string.Format(ErrorToString,ex.Message);
                }
                this.Add(ap);
            }
    
  3. The dog-food is good: I have followed my advice from msprogrammer.serviciipeweb… and it works ( used for Remove )
       [HttpPost]
            public JsonResult DeleteItem(string TheKey,int Source)
            {
                try
                {
                    var lad = new ListAppData();
                    lad.DeleteItem(TheKey,(SourceData)Source);
                    return Json(new { ok = true,message = "" });
                }
                catch (Exception ex)
                {
                    return Json(new { ok = false,message = ex.Message });
                }
                
            }
    
  4. When you pass strings in Javascript,there is a simple way to encode: HttpUtility.JavaScriptStringEncode
    <a href="javascript:removeItem('@HttpUtility.JavaScriptStringEncode(Model.Key)','@((int)Model.source)','@id')">Remove</a>
    

Possible uses:

  1. For developers –  when they want to see what happens when a cache item no longer exists
  2. For developers – to put to site admins some simple tool to reload data from Cache/Application . Just edit the LoadAll function to load only Cache/Application Winking smile
  3. For developers  – to test easily the session. Just delete ASP.NET_SessionId  cookie – you will get another one when you refresh the page.

You can view online at http://clearer.apphb.com/Clearer
The project could be found at http://clearer.codeplex.com and have all – source code,downloadable project .

Next week it will be a Nuget item.

For more features,please leave me a comment here or on codeplex at issues
Nuget package at http://nuget.org/packages/Clearer


Posted

in

by

Comments

5 responses to “Clearer MVC”

  1. Tudor Avatar
    Tudor

    Feature suggestion: an easy way to clear the AppFabric cache (assuming the user has the permissions to do this) – useful if I’m lazy to learn the powershell commands 🙂

    1. Andrei Ignat Avatar
      Andrei Ignat

      Tudor – can you do this? Please… any improvement is welcome

  2. mitsbits Avatar
    mitsbits

    simple and to the point, as it should be. Thanks for a neat solution to an everyday problem.

    1. Andrei Ignat Avatar
      Andrei Ignat

      Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *