No this is not a tribute to my favorite 80's movie Ghost busters....
Managing state keys
A popular tip I often give when delivering asp.net classes is to not let "free text" session strings hang around your system. Keys managed in this way will certanly cause trouble in the development lifecycle.
It's quite hard to keep track of them in a single human memory and even more difficult to actually synchronize the information across multiple human memory banks (maybe SOA could help solve the human to human memory synchronization issues ;)
This often leads to frustration and endless searches for the correct key data. Also misspelling at late friday nights are very common and leads to even more frustration.
The tip I usually give is to use constants instead. Constants stored in a central location, namely a Singleton class.
Quick example;
public sealed class KeyMaster
{
private KeyMaster(){}
public sealed class Session
{
private Session() {}
///
/// A nifty ey to access the nifty session object.
///
public const string MyNiftyKey = "MyNiftySessionKey";
}
public sealed class ViewState
{
private ViewState() {}
///
/// A nifty ey to access the nifty session object.
///
public const string MyNiftyKey = "MyNiftyViewStateKey";
}
public sealed class Cache
{
private Cache() {}
///
/// A nifty ey to access the nifty session object.
///
public const string MyNiftyKey = "MyNiftyCacheKey";
}
}
This will allows to easally access the keys in the form of:
aNiftyObject = (ANiftyType)Session[KeyMaster.Session.MyNiftyKey];
Which gives us three main advantages:
1. No more misspelling, I will get full intellisens for my keys with additional descriptions. This will clearly state what the key is used for.
2. It will be much harder to missuse a key by accident and much easier to find the appropiate key,
3. My development team will sleep better at friday nights.
While we're at it, let's talk about something very similar that also will help in handling the state bags.
Encapsulation of state reading / writing.
If I take it as practice to always encapsulate the retrievment of state objects into a static property, I will have a nicely refactored facility to reuse through out my entire application.
code:
public class FlashCard
{
public static FlashCard SessionFlashCard
{
get
{
return (FlashCard)HttpContext.Current.Session[KeyMaster.Session.FlashCard];
}
set
{
HttpContext.Current.Session[KeyMaster.Session.FlashCard] = value;
}
}
}
Maybe these tips will give help reduce frustration in your development life time cycle. Safe googling....
|