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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:03:55+00:00 2026-06-01T14:03:55+00:00

I wanted to get some feedback on your thoughts of storing entire objects into

  • 0

I wanted to get some feedback on your thoughts of storing entire objects into a session? For example, a customer object. Once a customer logs into their control panel, instead of re-fetching their data each time it is needed from a ID stored in a session, what are the pros and cons of storing the entire customer object in a session?

So anytime you needed to reference the customer object, you could just do:

Customer c = (Customer)Session["objCustomer"];

Obviously, you would need to check and have a function that refreshes the session. You would need to run this function in the case of a null session or after an update is made.

Aside of that, is there any other issues I should be aware of doing it this way? It seems this would be much better as far as programming and also make far less calls to the database. Thoughts anyone?

  • 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-06-01T14:03:56+00:00Added an answer on June 1, 2026 at 2:03 pm

    This is not a good idea.

    The main reason for that is the way ORMappers works – they don’t like it when you mix objects from different contexts. And this would happen in the scenario you propose – you have an object in the session, created by a different context your next (and following) requests use. Sonner or later you would start to get exceptions.

    Personally I prefer an approach where a simple object containing the Customer’s ID (and probably other attributes) is stored in a session (or in the custom data section of the forms cookie) and I wrap the access to the customer object in a simple statement which involves the Items container:

    (a production code would require few checks here and there to be more defensive):

    const string CUSTOMERITEM = "customeritem";
    public Customer Current
    {
        get
        {
            if ( HttpContext.Current.Items[CUSTOMERITEM] == null )
            {
               int id = retrieve_the_id;
    
               using ( DbContext ctx = GetCurrentDbContext() ) 
               {
                   HttpContext.Current.Items.Add( CUSTOMERITEM, ctx.Customers.FirstOrDefault( c => c.ID == id );
               }
            }
            return (Customer)HttpContext.Current.Items[CUSTOMERITEM];
        }
    }
    

    Items container lasts only for the time of one request. The snippet above makes sure that the object is loaded only once per request.

    Of course, this comes with a price of one additional query per request comparing to your approach. But the advantage is that you never mess up database contexts.

    Note that there are some business processes which don’t require the Customer object but you can pass the customer’s ID directly and use in queries:

     public IEnumerable<Order> CustomerOrders( int CustomerID ) 
     {
         // use the customer id directly, without first loading the customer object
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to get some assistance into how to set up new rulesets for
Im adding facebook one-login to my website. Just wanted to get some feedback from
I wanted to get a feedback from you guys (and hopefully some references to
I was looking at this article from 2005 and wanted to get some thoughts
I got the Flot-created graph. What I wanted to acomplish is to get some
I am working on some scraping app, i wanted to try to get it
I wanted to get the words from a cell. For example, cell A2 has
I've been struggling with queries with Google's datastore and wanted to get some help.
Hey guys I wanted to know if I get get some advice from some
I am looking to have some values for a string, and wanted to get

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.