I have a WPF application using MVVM approach.
In one of my viewmodels I have a private observable collection which I set to a ‘new observableCollection’ which is created from a query which fetches the data via a repository class using an ISession object from NHibernate.
I have a private observable collection in my viewmodel which I want to update with the collection returned/created from my query.
The problem is, every time I call my method to perform the query, the time taken to update the private object ( ObservableCollection ) takes longer every time.
I tested the time taken for the query to execute in the repository and its only 4 or 5 seconds.. I am retrieving 4500 records using lazy loading.
So my question is why would the time increase every time I call my refresh method in the viewmodel? Additionally I should also add that the memory usage shown in task manager also increases so it almost appears there is a memory leak?
Code example below:
ViewModel:
private static ObservableCollection<CustomerViewModel> _allCustomers;
Expression<Func<Customer, bool>> expression2 = p => p.IsVisible == 'T';
_allCustomers = new ObservableCollection<CustomerViewModel>(
from customer in ManyEntitiesRepository<Customer>.Instance.RetrieveAll(expression2)
select new CustomerViewModel(customer));
Repository Retrieve
public virtual IList<T> RetrieveAll(Expression<Func<T, bool>> expression)
{
try
{
if (_allEntities != null)
{
foreach (T entity in _allEntities)
SessionProvider.Instance.GlobalSession.Evict(entity);
_allEntities.Clear();
}
_allEntities = (from c in SessionProvider.Instance.GlobalSession.Query<T>().AsExpandable()
where expression.Invoke(c)
select c).ToList<T>();
return _allEntities;
}
To workaround this I am looping the viewmodels in my private collection and calling cleanup() on each… adds an extra 4 seconds or so to the refresh but hey its better than having your memory clogged up with duplicate viewmodels and having the query time increase by 20 seconds or more with each refresh.
I will keep an eye out for V.4 of the toolkit release in the coming months…
Cheers