Following the DRY Principle in ASP.NET

I recently participated in a classic ASP.NET project that contains many values ​​to store and read from session and query strings. It might look something like this:

Session["someKey"]=someValue;

And somewhere else in the code, the value in the session is read. Clearly, this violates the DRY principle, as you will have a literal string key distributed throughout the code. One way to avoid this could be to store all keys as constants that can be referenced wherever there is a need to read and write to the session. But I'm not sure if this is the best way to do this. How would you recommend me best deal with this so that I don't violate the DRY principle?

+5
source share
4 answers

, ,

public class SessionVars
{
   public const string SOME_KEY = "someKey";
   public const string SOME_OTHER_KEY = "someOtherKey";
}

:

Session[SessionVars.SOME_KEY]=someValue;

IntelliSence .

+7

, DRY. , . , , 5 , .

( ), .

+2

, , :

using System;
using System.Web;

namespace Project.Web.UI.Domain
{
    public abstract class SessionBase<T> where T : class, new()
    {
        private static readonly Object _padlock = new Object();

        private static string Key
        {
            get { return typeof(SessionBase<T>).FullName; }
        }

        public static T Current
        {
            get
            {
                var instance = HttpContext.Current.Session[Key] as T;

                lock (SessionBase<T>._padlock)
                {
                    if (instance == null)
                    {
                        HttpContext.Current.Session[Key] 
                          = instance 
                          = new T();
                    }
                }
                return instance;
            }
        }

        public static void Clear()
        {
            var instance = HttpContext.Current.Session[Key] as T;
            if (instance != null)
            {
                lock (SessionBase<T>._padlock)
                {
                    HttpContext.Current.Session[Key] = null;
                }
            }
        }
    }
}

. , . . , , :

public class MyClass
{
    public MyClass()

    public string Blah1 { get; set; }
}

Then along the way you expand MyClass, and you do not need to remember all the key values, store them in AppSettings or Const variables in static classes. You simply determine what you want to save:

public class MyClassSession : SessionBase<MyClass>
{ 
}

And anywhere in your program, you simply use the class.

// Any Asp.Net method (webforms or mvc)
public void SetValueMethod()
{
  MyClassSesssion.Current.Blah1 = "asdf";
}

public string GetValueMethod()
{
  return MyClassSession.Current.Blah1;
}
+1
source

Optionally, you can place access to this session object on the base page and wrap it in the property:

class BasePage : Page
{
   ...
   public string MySessionObject
   {
      get
      {
         if(Session["myKey"] == null)
            return string.Empty;
         return Session["myKey"].ToString();
      }
      set
      {
          Session["myKey"] = value;
      }
   }
   ...
}

Here you repeat the string myKey, but it is encapsulated in the property. If you want to avoid this as a last resort, create a constant with the key and replace the string.

0
source

All Articles