Friday, December 21, 2012

Creating the .NET Solution Part 3

Session

The session object is another item that is frequently used in most web applications. That being said, it is also something that we can squeeze performance out of down the road. Even if performance wasn't an issue, the session object by itself does not really conform to the most basic of OOP principles. Rather than trying to cast an item out of thin air or the HttpContext.Current.Session, it would be much better if we could call an object that returned the appropriate object for us. Here is the basic implementation:


  1. We will continue from last post Creating the .NET Solution Part 2 open the Fishbook solution.
  2. Right click in Interfaces project and select Add/New Item.
    create IWebContext interface
    create IWebContext interface
    1. Select Interface Item from C# Template and name it IWebContext.cs and then write the following code:
      namespace Fishbook.Interfaces
      {
          public interface IWebContext
          {
              void ClearSession();
              bool ContainsInSession(string key);
              void RemoveFromSession(string key);
          }
      }
    2. Right click in Components project and select Add/New Item.
      create WebContext class
      create WebContext class
      1. Select Class Item from C# Template and name it WebContext.cs and write the following code:
        using System.ComponentModel.Composition;
        using System.Web;
        using Fishbook.Interfaces;

        namespace Fishbook.Components
        {
            [Export(typeof(IWebContext))]
            public class WebContext : IWebContext
            {
                public void ClearSession()
                {
                    HttpContext.Current.Session.Clear();
                }

                public bool ContainsInSession(string key)
                {
                    return HttpContext.Current.Session[key] != null;
                }

                public void RemoveFromSession(string key)
                {
                    HttpContext.Current.Session.Remove(key);
                }

                private string GetQueryStringValue(string key)
                {
                    return HttpContext.Current.Request.QueryString.Get(key);
                }

                private void SetInSession(string key, object value)
                {
                    if (HttpContext.Current == null || HttpContext.Current.Session == null)
                    {
                        return;
                    }
                    HttpContext.Current.Session[key] = value;
                }

                private object GetFromSession(string key)
                {
                    if (HttpContext.Current == null || HttpContext.Current.Session == null)
                    {
                        return null;
                    }
                    return HttpContext.Current.Session[key];
                }

                private void UpdateInSession(string key, object value)
                {
                    HttpContext.Current.Session[key] = value;
                }
            }
        }

      We will continue to extend this object to have specific methods that could handle features. Say we stored a person in the session as the current user. We could have a GetCurrentUserFromSession() method defined that would interact with our wrapper methods. It would retrieve the user and cast the object as a person. This is much better OOP-wise. Let's look at the wrapper and for brevity, we will only look at key or new aspects in this code.
      Let us look at some of the public method definitions. These are all pretty easy to understand and primarily work with the HttpContext object. We have a ClearSession() method that simply resets the session. We have a ContainsInSession() method that takes a key value. It checks whether that key is present in the session and returns true or false. The RemoveFromSession() method that takes in a key and attempts to remove that key from the session.
      After our public methods, we have a few private methods, namely, GetQueryStringValue(), SetInSession(), GetFromSession(), and UpdateInSession(). Before we discuss these methods, we need you to understand why they are private. We could make all of these public and they would work just fine. However, making them public would also mean that we would scatter the code about our application that directly interacts with the session. Our preference is that we extend this object to provide more specific methods that work with these private methods, that in turn work with the session. This provides us a bit more encapsulation regarding the session interaction.
      The GetQueryStringValue() method takes in a key value and retrieves the item from the query string. The SetInSession() method allows you to pass in a key and an object. The object is then stored in the session under that key name. GetFromSession() does just that—it takes a key and retrieves that corresponding object. UpdateInSession() is very similar to SetInSession() with the exception that it assumes that a key already exists and updates the value that is currently stored there. This method will throw an error if a key does not exist. Therefore prior to using this method, you should check that your key exists in the session collection!

      No comments:

      Post a Comment

      Automatic Traffic Exchange

      YallaTech Facebook page