Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 955237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:20:52+00:00 2026-05-16T00:20:52+00:00

I have a class called EditMapUtilities. Here are some class properties that I want

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:20:53+00:00Added an answer on May 16, 2026 at 12:20 am

    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 EditMapUtilites and 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.

    public class EditMapUtlities
    {
        //Omitting the wireup details for now
        private SessionHandler ApplicationState {get; set;}
    
        public static Boolean isInitialEditMapPageLoad
        {
            get { return ApplicationState.GetValue<Boolean>("EditMapInitialPageLoad"); }
            set { ApplicationState.SetValue("EditMapInitialPageLoad", value); }
        }
    
    // REST OF CLASS NOT GERMAIN TO DISCUSSION AND OMITTED
    }
    

    And the SessionHandler class would now contain two new methods:

    public static T GetValue<T>(String key)
    {
        return currentSession[key] ?? default(T);
    }
    
    public static void SetValue(String key, Object val)
    {
        currentSession[key] = val;
    }
    

    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 SessionHandler class 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:

    public interface IApplicationState
    {
        public T GetValue<T>(String key);
        public SetValue(String key, Object val);
    }
    

    Now we can simply change over our property inside of EditMapUtlities to use the interface, and pass this in through the constructor using good ol’ manual Dependency Injection.

    public class EditMapUtlities
    {
        //Omitting the wireup details for now
        private IApplicationState ApplicationState {get; set;}
    
        public EditMapUtilities(IApplicationState appState)
        {
            if(appState == null) throw new ArgumentNullException("appState");
    
            ApplicationState = appState;
        }
    
        public static Boolean isInitialEditMapPageLoad
        {
            get { return ApplicationState.GetValue<Boolean>("EditMapInitialPageLoad"); }
            set { ApplicationState.SetValue("EditMapInitialPageLoad", value); }
        }
    
    // REST OF CLASS NOT GERMAIN TO DISCUSSION AND OMITTED
    }
    

    You can easily refactor your SessionHandler class to implement the interface and not be static:

    public SessionHandler: IApplicationState { //Implementation }
    

    Or if you need to maintain backward compatibility with other areas of code, you could create a wrapper class, and leave SessionHandler static:

    public SessionHandlerWrapper: IApplicationState
    {
        //Call SessionHandler from within this class
    }
    

    Now it’s true that you will have to manually create your concrete instance of IApplicationState and pass it into EditMapUtilities, 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 implemented IApplicationState. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.