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 6358819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:26:15+00:00 2026-05-24T23:26:15+00:00

With the way my site is setup, I have a master page, page and

  • 0

With the way my site is setup, I have a master page, page and then controls within that page, all that can display information about the viewing user.

Because I am requiring this information quite a few times, I am having to create an instance of my model, then returning the User object for each time I need to read data from their account.

As a way to combat this, I done the following:

public static class Core
{
    private static PatientRow Patient;
    public static PatientRow GetCurrentPatient()
    {
        if (Patient == null && HttpContext.Current.User.Identity.IsAuthenticated && HttpContext.Current.User.IsInRole("Patient"))
        {
            using (PatientModel model = new PatientModel())
            {
                Patient = model.Find<PatientRow>(User.CurrentUser.UserID);
            }
        }
        return Patient;
    }
}

As you can see, I’m check to see if the private static variable Patient is null, if so, going away and getting it. The next time I call GetCurrentPatient, as Patient is already populated, I’ve just saved my self a database call etc.

Now comes the issue… As we all know, this will mean every time I call GetCurrentPatient, no matter who is viewing the site, they will get the last (or first) set value.

I don’t want to be storing objects in the Session, it’s naughty, but I do need to find a way to either restrict the static scope to the User’s session, or, a reasonable comprimise, to restrict it to the current page’s execution.

Any suggestions?

Cheers

Edit: Prior to reading Darin’s answer, I had implemented the following.

Every Page I have, inherits a BasePage class (to make things easier for handling menu’s etc). As any code stored in here is only available during the execution (with the exception to static variables), I simply created a Registry Dictionary collection and then from my originating GetCurrentPatient, I simply checked if the registry contained that object, if so, returned it, if not got it, stored it and finally returned it.

I’ll be making changes to my current code to match that of Darin’s. Much appreciated Darin!

  • 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-24T23:26:16+00:00Added an answer on May 24, 2026 at 11:26 pm

    You could store it inside the HttpContext.Current.Items bag. It will be available during the entire execution of an HTTP request and obviously restricted only to the current user. So if you call the GetCurrentPatient method multiple times from multiple places within the same request it will hit the database only once. If you need to persist this information for more than a request you need to use Session.

    For example:

    public static class Core
    {
        public static PatientRow GetCurrentPatient()
        {
            var patient = HttpContext.Current.Items["patient"];
            if (patient == null && HttpContext.Current.User.Identity.IsAuthenticated && HttpContext.Current.User.IsInRole("Patient"))
            {
                using (PatientModel model = new PatientModel())
                {
                    patient = model.Find<PatientRow>(User.CurrentUser.UserID);
                    HttpContext.Current.Items["patient"] = patient;
                }
            }
            return patient;
        }
    }
    

    Don’t store anything in static fields especially if this information is tied to the current user only.

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

Sidebar

Related Questions

basically I have my site setup to display the title of each page/post in
Currently, I have a Site.Master page for my MVC app that renders great when
I have an ASP.NET MVC site (Master Page + Content pages). On the master
In almost every page in my ASP.NET MVC site I am using information about
I have my site setup so that if you are on a mobile you
The way we have our security setup in Sitecore, we have departmental roles that,
The way my site is setup, I need to manually visit two URLs to
We have online SharePoint site and we want to use tool or any way
I am not very experienced with PHP. I have a site I'm maintaining that
I have a git repository for a web site, where the master branch represents

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.