Part I
I am working on a web application that instead of using a viewmodel class to organize data and be used in View, uses a database table model. So for example in my view, model declaration looks like this: @model aa.webobjects.object – object is automatically generated by dbml. The question here is what is the benefit of using a viewmodel other than organizing data? I have another class, under dbml, that for example does this:
public Product GetProductsByPrice
{
get
{
return WebDataContext.Get().Products.Where(x => x.Price > 10);
}
}
And basically this is used to organize data, and pull data from database as needed. The return statement brings us to question two.
Part II
My application has a memory leak. The memory keeps growing and growing and after a while the server dies. As I read through some articles it seems like I should wrap my WebDataContext in using statement so that it’ll recycle properly. My question here is: is not using Viemodel causing the memory leak, ir if I used viewmodels to organize data, and reorganize usage of WebDataContext so that it’s wrapped in using statement would help?
I am not sure if this is explaining the problem correctly. Any help will be greatly appreciated.
For sure you have to dispose your DataContexts instances (that’s basically what using does). My suggestion is to add this in your Global.asax:
and make WebDatacontext.Get() to return it from HttpContext.Current.Items.
In this way, your datacontext will be available for the whole request and it will be disposed when you don’t need it anymore.
Hope it helps.