views:

24

answers:

1

I am working in an asp .net mvc application. I am using the model and storing some of the values which i need to preserve between the page posts, in the form of datacontexts.

Say my model looks something like this:

public SelectedUser SelectedUserDetails
{
    //get and set has 
    //this.datacontext.data.SelectedUser = .....
    //return this.datacontext.data.....
}

Now when this model needs to be cleared? I have many such models with many properties and datacontext. But I don't have an idea on when to clear it. Is there a way or an event that can be triggered automatically when the model is not used for a long time?

Oneway I thought is when i navigate away from a page which uses my underlying model, I can clear that model if its no longer used anywhere and initialize it back as needed. But I need to clear almost many models at many points. Is there an automatic way that can clear models when it is no longer used because care can be taken by my code to initialize them when I need them, but I don't know when to clear them when I no longer need them. I need this to get rid of any memory related issues. Any thoughts or comments?

A: 

I would use the ASP.NET cache or the Session to keep data between requests. The cache timeout can be set on the object and it will automatically be removed -- note that you'll need a way to reconstitute it if it is removed before you are done using it. If you use the session, the objects will be removed when the session times out. You could also -- by default -- remove it (or replace it) when you hit an action that would start the sequence of actions for which it is needed.

tvanfosson
Thanks. It helps...
SARAVAN