DomainDataSource Extension Methods

by Alex Meyer-Gleaves 13 August 2009 - 1:48 AM

To add a new item or edit an existing item in the DomainDataSource that ships with the .NET RIA Services, you must first cast the DataView property to an IEditableCollectionView. Jeff Handley does a good job of explaining the reason for this in his DomainDataSource.DataView post.

David Yack left a comment on the .NET RIA Services forum with a link to his post where he shares his inherited DomainDataSource class. The inherited class adds methods for adding and editing items that do all the casting to IEditableCollectionView for you. I really liked the idea but didn’t want to create my own derived class, so I took his implementation and turned it into a set of extension methods that are exposed on all DomainDataSource instances.

Update (24-09-2009): Added a Remove extension method. Thanks to Phil Steel for posting the code in his comment.

/// <summary>
///    Extensions to the <see cref="DomainDataSource"/> for adding and editing items.
/// </summary>
public static class DomainDataSourceExtensions
{
    /// <summary>
    /// Adds a new item to the collection.
    /// </summary>
    /// <typeparam name="T">The type of the item to add.</typeparam>
    /// <param name="source">The <see cref="DomainDataSource"/> being extended.</param>
    /// <returns>The newly added item.</returns>
    public static T AddNew<T>(this DomainDataSource source)
    {
        IEditableCollectionView collection = ((IEditableCollectionView)source.DataView);
        return (T)collection.AddNew();
    }

    /// <summary>
    /// Edits an item in the collection.
    /// </summary>
    /// <param name="source">The <see cref="DomainDataSource"/> being extended.</param>
    /// <param name="itemToEdit">The item to edit.</param>
    public static void EditItem(this DomainDataSource source, object itemToEdit)
    {
        IEditableCollectionView collection = ((IEditableCollectionView)source.DataView);
        collection.EditItem(itemToEdit);
    }

    /// <summary>
    /// Removes an item from the collection.
    /// </summary>
    /// <param name="source">The <see cref="DomainDataSource"/> being extended.</param>
    /// <param name="itemToRemove">The item to remove.</param>
    public static void Remove(this DomainDataSource source, object itemToRemove)
    {
        IEditableCollectionView collection = ((IEditableCollectionView)source.DataView);
        collection.Remove(itemToRemove);
    }

    /// <summary>
    /// Commits the add or edit transaction.
    /// </summary>
    /// <param name="source">The <see cref="DomainDataSource"/> being extended.</param>
    public static void CommitNewAndEdit(this DomainDataSource source)
    {
        IEditableCollectionView collection = ((IEditableCollectionView)source.DataView);
        if (collection.IsAddingNew)
        {
            collection.CommitNew();
        }
        if (collection.IsEditingItem)
        {
            collection.CommitEdit();
        }
    }

    /// <summary>
    /// Cancels the add or edit transaction.
    /// </summary>
    /// <param name="source">The <see cref="DomainDataSource"/> being extended.</param>
    public static void CancelNewAndEdit(this DomainDataSource source)
    {
        IEditableCollectionView collection = ((IEditableCollectionView)source.DataView);
        if (collection.IsAddingNew)
        {
            collection.CancelNew();
        }
        if (collection.IsEditingItem)
        {
            collection.CancelEdit();
        }
    } 
}

Thanks for sharing the code David.

Tags: ,

Categories: Silverlight | Web Development

Comments

9/23/2009 2:02:01 AM #

Phil Steel

Hi Alex,

Very handy. I also added an extension to support deletion of items:

public static void Remove(this DomainDataSource domainDataSource, object itemToRemove)
{
  var collection = ((IEditableCollectionView)domainDataSource.DataView);
  collection.Remove(itemToRemove);
}

Cheers,
Phil

Phil Steel United Kingdom |

9/23/2009 2:24:59 AM #

Alex Meyer-Gleaves

Hi Phil,

Thanks for the code snippet. I have updated my code to include your Remove extension method.

Cheers,

Alex.

Alex Meyer-Gleaves Australia |

5/11/2010 10:17:16 PM #

Matjaz Bravc

Very useful! Excellent!

Matjaz Bravc Slovenia |

Comments are closed

About the author

Alex Meyer-Gleaves I'm a software developer living in Australia (that island like continent in the southern hemisphere). I love Microsoft .NET and C#. I hate early mornings, slow drivers and Lotus Notes.

Google Reader Clips

SpringWidgets
RSS Reader
This widget is the staple of our platform. Read all your feeds right here with thisone widget - Supported feeds are OPML, RSS, RDF, ATOM. Watch your favorite Podcastin the embedded Video Player on the Desktop or publish your own video playlist toyour site for others to view!

Recent Comments

Comment RSS

Links

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008