RIA Services DomainDataSource.Data not updating
One thing I noticed in the July CTP of .NET RIA Services was that my Data Grids were no longer updating automatically when I added records via the associated DomainContext. This is due to an intentional change the team made to make the DomainDataSource play better with a broader set of scenarios. Jeff Handley has a good post on the forums ( here ) that explains the change in more detail and how to handle it going forward.
Basically, the DomainDataSource (DDS) will now only display entities it knows about. So as Jeff says in his post, only the top level entities that came back from a load operation or those added through the DDS.DataView collection. So then you ask, how do I add records that can show up immediately without having to refresh the DDS. Using the IEditibleCollectionView interface on the DataView property you can use the AddNew method to create an instance the entity. When your done with the records you need to call the CommitNew or CancelNew depending on what you want to happen with that object. There’s also a set of methods for edit – EditItem, CommitEdit, CancelEdit for when your working with modifying.
First of all, the IEditableCollectionView is located now in the System.Windows.Data assembly. When I first tried to get this all working I had to hunt around a bit to find that. Because the DDS DataView property is natively typed as ICollectionView you must cast it to an IEditableCollectionView to get access to the above methods. var myNewObj = ((IEditableCollectionView)myDDS.DataView).AddNew() as Course;
After doing that a few times, I got tired of all the casting and have suggested they wrap that up to be a little more friendly to use. Until that happens, I have a MyDomainDataSource that inherits from DomainDataSource that I threw in a few helper methods to make it a little cleaner to work with. Here’s the methods – feel free to use them if they will help you out. One thing you will notice is I combined the Commit and Cancel methods for Add and Edit. I’ve yet to track down a good reason that they are separate and require you to decide which to call upon completion of a modification session against the object.
public DataType AddNew<DataType>()
{
var ecv = ((IEditableCollectionView)this.DataView);
return (DataType) ecv.AddNew();
}
public void EditItem(object itemToEdit)
{
var ecv = ((IEditableCollectionView)this.DataView);
ecv.EditItem(itemToEdit);
}
public void CommitNewAndEdit()
{
var ecv = ((IEditableCollectionView)this.DataView);
if (ecv.IsAddingNew)
ecv.CommitNew();
if (ecv.IsEditingItem)
ecv.CommitEdit();
}
public void CancelNewAndEdit()
{
var ecv = ((IEditableCollectionView)this.DataView);
if (ecv.IsAddingNew)
ecv.CancelNew();
if (ecv.IsEditingItem)
ecv.CancelEdit();
Reader Comments (3)
David,
Thanks for posting this and thanks for the feedback!
-Jeff Handley
Hi David,
Thanks for sharing your code. I really like the idea and have turned your code into a set of extension methods that can be added to all DomainDataSource instances.
http://alexmg.com/post/2009/08/13/DomainDataSource-Extension-Methods.aspx
Cheers,
Alex.
Hey Alex, yeah extension methods are a great option as well