I have a class called EditMapUtilities.
Here are some class properties that I want to persist:
public class EditMapUtlities
{
public static Boolean isInitialEditMapPageLoad
{
get { return SessionHandler.isInitialEditMapPageLoad; }
set { SessionHandler.isInitialEditMapPageLoad = value; }
}
// REST OF CLASS NOT GERMAIN TO DISCUSSION AND OMITTED
}
Here is my SessionHandler Class following the pattern from this post
Static Session Class and Multiple Users:
using System.Web.SessionState;
public static class SessionHandler
{
private static HttpSessionState currentSession
{
get
{
if (HttpContext.Current.Session == null)
throw new Exception("Session is not available in the current context.");
else
return HttpContext.Current.Session;
}
}
//A boolean type session variable
private static string _isInitialEditMapPageLoad = "EditMapInitialPageLoad";
public static bool isInitialEditMapPageLoad
{
get
{
if (currentSession[_isInitialEditMapPageLoad] == null)
return true;
else
return (Boolean)currentSession[_isInitialEditMapPageLoad];
}
set
{
currentSession[_isInitialEditMapPageLoad] = value;
}
}
}
I am still learning OOAD. I want to keep relevant properties with relevant classes. I also want to keep all Session stored variables in one place for ease of maintenance and to encapsulate the session keys and calls.
I feel like my design is too coupled though. How can I make it more loosely coupled? Is my editMapUtilities class too tightly coupled to the SessionHandler class? How would you do it better?
Your intuition is pretty good in that your classes are indeed very tightly coupled to one another. I’m going to be referencing Bob Martin’s Principles of Object Oriented Design.
Following the Single Responsibility Principle, you want to make sure that you don’t have to update both of these classes if one of them changes. Right now both classes know a little too much about each other.
We can start with
EditMapUtilitesand refactor access to the session handler to more accurately convey the intent. Mainly, application state scoped to the user. Also, we can change how we access those values to make it less coupled by using methods that accomplish the same goal.And the
SessionHandlerclass would now contain two new methods:Ok, so one class is now slightly more verbose, but your session class is simpler and more flexible. Adding additional values is simple, but you may ask yourself why not just use straight session?
The reason we restructured the classes this way was to get us one step closer to a clean break between them. By accessing our
SessionHandlerclass through a property we can now take our refactoring one step further and employ the Dependency Inversion Principle.Instead of relying on a concrete implementation of
SessionHandler, why don’t we abstract the storing of Application State variables behind an interface. One that looks like this:Now we can simply change over our property inside of
EditMapUtlitiesto use the interface, and pass this in through the constructor using good ol’ manual Dependency Injection.You can easily refactor your
SessionHandlerclass to implement the interface and not be static:Or if you need to maintain backward compatibility with other areas of code, you could create a wrapper class, and leave SessionHandler static:
Now it’s true that you will have to manually create your concrete instance of
IApplicationStateand pass it intoEditMapUtilities, but now neither class is dependent on the other. In fact, if you ever wanted to store this in the database in the future, you would simply have to write a new class that implementedIApplicationState. Only the calling code would have to change to use the new database class instead of the session based one.Now you can enjoy your new loosley coupled architecture and the minimized changes you will have to make to add new functionality.