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?