Print Page | Close Window

AllInDataSourceAsync with includes

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4132
Printed Date: 04-May-2024 at 6:49am


Topic: AllInDataSourceAsync with includes
Posted By: cefernan
Subject: AllInDataSourceAsync with includes
Date Posted: 30-Apr-2013 at 12:32pm
Hi guys,

Let me try to explain my scenario. 

I have two ViewModels working with different unit of works and entity managers.
In the first one, I have a list of entity loaded using this method:
private async Task<BindableCollection<Something>> LoadData()
{
    var repository = UnitOfWork.GetRepository<Something>();
    var entityData = await repository.AllInDataSourceAsync(fetchOptions: f => f.Include(i => i.SomethingProperties));     return new BindableCollection<Something>(entityData);
}
In this first ViewModel, I have a button that calls the second ViewModel (childWindow). In the second ViewModel I'm able to create, edit or delete data from entity Something and SomethingProperties.
So, when deactivate the childWindow I call LoadData() again to refresh my list. As I use AllInDataSourceAsync the update happens perfectly in the entity Something, but the navigation (SomethingPropertiesshows objects that I have deleted (???) with them that I have just created (ok).

PS: Before close the childWindow I have saved changes in the second entity manager. If I check the database, it is everything ok.

What is my mistake?




Replies:
Posted By: cefernan
Date Posted: 02-May-2013 at 6:46am
If I explicity do this below in the first entity manager before call LoadData, SomethingProperties is updated correctly.
UnitOfWork.EntityManager.Clear();
Is it ok?


Posted By: mgood
Date Posted: 02-May-2013 at 12:33pm
This is normal. The deleted entities are still in the cache of the first EntityManager. Nothing tells it that those entities got deleted. When you call AllInDataSourceAsync you simply get updated data for the entities that still exist, but the deleted entities remain in the cache, because the EntityManager doesn't just assume they were deleted. You can enable change syncing in Cocktail to inform all EntityManagers of the changes you saved in the second ViewModel. This will remove the deleted entities from every EntityManager that holds a copy in its cache. 

http://drc.ideablade.com/devforce-2012/bin/view/Documentation/cocktail-entitymanager-provider#HSynchronizingchanges - http://drc.ideablade.com/devforce-2012/bin/view/Documentation/cocktail-entitymanager-provider#HSynchronizingchanges

As an alternative to the example in the documentation, consider the implementation in TempHire. In addition to the example in the doc, the TempHire implementation also handles the case where in your example you add new entities to the SomethingProperties and want them to appear in the first EntityManager.

https://github.com/IdeaBlade/Cocktail/blob/master/Samples/TempHire/TempHire/SyncInterceptor.cs - https://github.com/IdeaBlade/Cocktail/blob/master/Samples/TempHire/TempHire/SyncInterceptor.cs





Posted By: cefernan
Date Posted: 02-May-2013 at 2:33pm
I got it!

Thank you very much Marcel.



Print Page | Close Window