开发者

when to clear or make null asp.net mvc models?

开发者 https://www.devze.com 2022-12-23 06:29 出处:网络
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.

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 tha开发者_JAVA技巧t 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消