Model in sessions

Asked

Viewed 85 times

2

I have a GRID (table) that when selecting an item through the ID I load the entity in a session via ajax.

public static class Sessoes
    {
        public static Produto Produto
        {
            get
            {
                return (Produto)HttpContext.Current.Session["Produto"];
            }
            set
            {
                HttpContext.Current.Session["Produto"] = value;
            }
        }
    }

I wonder if it would be a bad decision to save every entity in a session? What do you think? Is there/know any disadvantage in using this practising?

1 answer

2


Has several drawbacks.

In this link, for example, the author teaches how to easily capture data from a session.

By putting his Produto within a Session, you lose lazy load capacity because the assignment will cause the object to be highlighted from the context. Changes to it will also not be mapped.

There is also the aggravation of using your application in a distributed environment, with load balancing (Azure environments, other than virtual machines, for example). Using this approach, your application will fail at runtime as Sessions have not been well implemented for this type of environment.

To Session needs to be viewed as a temporary cache per user, and not as a general data storage resource, as is, for example, a ViewBag or a dictionary ViewData.

  • Here at the company where I work is having a discussion about this... In my opinion we should save the ID and take the entity by the ID, on account of a line of code to return the entity is giving a rifle! Soon I accept the answer, let’s see if anyone else puts something!

  • @Correct Trxplz0. By Id, there are no problems. The important thing is not to keep as much data on a temporary and volatile object as the Session.

  • Gypsy, about the "Sessions not out well... environment" part, you mean in-process right? Yeah, if I’m not mistaken, the Asp.NET State Service is exactly for that it’s not?

  • @Trxplz0 I don’t know. I’ve never used this feature. By the way, it would be good if I took a look at it.

  • Anyway, it is not good to save an entity in Session!

  • @Trxplz0 Exactly.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.