Press "Enter" to skip to content

Using Dynamic Data in Xamarin Forms (Part 1)

I have recently been exploring DynamicData and since I started using it, I can say that it has been a game changer in my applications. It is quite easy to use and provides a lot of flexibility when working with collections. So, I decided to write a series of articles about it:

  • Getting Started 
  • Filtering 
  • Grouping
  • Much More… 

Getting Started

What’s dynamic data?

Dynamic Data is a portable class library which brings the power of Reactive Extensions (Rx) to collections.”. With it, you will be able to perform complex operations such as a filter, grouping, add range, transform elements, etc in an effortless way.

How to use it?

For the following example, we are going to build a list of Restaurants. You can find the class used here.

1. Install the DynamicData Package in your Xamarin Forms project

2. Create an IObservable<ChangeSet>

There are two ways to create it:

  1. SourceCache<TObject, TKey> for items that have a unique key.
  2. SourceList<T> for items that don’t have a unique key.

In this specific case, I will use a SourceCache since each Restaurant has a unique id. t is also important to know that SourceCache performs much better than SourceList, so it is always wise to consider using it.

As you see when using the SourceCache I need to specify the type of my collection and its key.

3. Define the collection that you will use to display your items in the UI

4. Add items to the collection

In Dynamic Data to add items, you will add them to the SourceCache that we defined in step 2.

5. Connect the SourceCache and output the result to your collection by using Bind method.

_cleanUp is a variable to dispose the SourceCache

The full ViewModel code looks like this:

6. Use the collection in your UI

How to Add/Edit elements

The SourceCache provides a method to Update/Edit if the element exists it will update it, if not it will create a new one and add it to the list.

NOTE: As you see you don’t need to re-assign it to the collection you are using in the UI, you just need to add it to the cache.

How to delete elements

Result

If you haven’t fallen in love with DynamicData yet, stay tuned for the next part of this series on Filtering, where you’ll see how easy it is to filter items.

Check the full source code here.

Happy Dynamic Data!

9 Comments

  1. LEO SOUZA LEO SOUZA

    Nice job !
    But what is the advantage of dynamic data over observable collections ?

    • Rendy Rendy

      Keep posted to the series and you will see 🙂

      But the observable collection needs to be reassigned when doing filtering or sorting with Dynamic Data that is handled seamlessly without reassigning the collection.

  2. Lubos Lubos

    Hello,

    Thankss for interesting example. Are data stored in sourceCache persisted between app restarts? What happen to data when you add a property to the Restaurant entity after some time? Is there some possibility to migrate data?

    Thank you

    • Rendy Rendy

      No, it is in-memory cache

  3. Thomas Thomas

    Thanks for sharing!

  4. Angelru Angelru

    Thanks for this discovery, I understand that update.AddOrUpdate updates the whole model and updates in the view without implementing inotifypropertychanged in the model, it works for individual properties of the model?

  5. Angelru Angelru

    Thanks for this discovery, I understand that update.AddOrUpdate updates the whole model and updates in the view without implementing inotifypropertychanged in the model, it works for individual properties of the model?

  6. Roland Pheasant Roland Pheasant

    Great post.

    Fyi, you not need the RefCount() after Connect() as it is is only required for shared connections i.e, when multiple consumers call Connect()

    • Rendy Rendy

      Thanks for clarifying. Nice to know!

Comments are closed.