DevExpress XPO XPCollection Refreshing Changes

SharpAffair picture SharpAffair · Apr 9, 2013 · Viewed 7.4k times · Source

I'm experiencing an issue refreshing data in XPCollection after commiting some database changes using UnitOfWork.

I have a WinForm with an XPCollection.

The XPCollection uses XpoDefault.Session.

I do some changes through a UnitOfWork:

using (UnitOfWork uow = new UnitOfWork())
    {
    var photos = new XPCollection<Photo>(uow);
    photos[0].Date = DateTime.Now;
    uow.CommitTransaction();

    }

To get the original XPCollection to update the changes, I've tried the following:

foreach (Photo photo in myXPCollection)
{
XpoDefault.Session.Reload(photo);
}

foreach (Photo photo in myXPCollection)
{
photo.Reload();           
}

myXPCollection.Reload()

None of the methods work.The changes are not reflected in the original XPCollection.

They are only visible when I start with a completely new Session. Obviously, this is a big performance problem.

How to get the changes made using UnitOfWork to another Session?

Answer

shamp00 picture shamp00 · Apr 9, 2013

You said:

They are only visible when I start with a completely new Session. Obviously, this is a big performance problem.

That's exactly what you should do. Create a new UnitOfWork each time you want refreshed data. A UnitOfWork is very cheap performance-wise to instantiate. If you have a large collection of Photo objects, you should improve performance by loading only the objects you need by specifying the Criteria parameter in the XPCollection<Photo> constructor.

When you issue Reload() it doesn't fetch anything from the database: it discards any changes and reloads the object from the session identity map. You can read this article and this Support Center issue for more information.

By the way, the DevExpress Support Center is by far the best place to ask DevExpress questions.