Skip to content

Instantly share code, notes, and snippets.

@RolandPheasant
Last active July 11, 2016 20:11
Show Gist options
  • Save RolandPheasant/9d5156f4743c28b14cc1e0feae157de7 to your computer and use it in GitHub Desktop.
Save RolandPheasant/9d5156f4743c28b14cc1e0feae157de7 to your computer and use it in GitHub Desktop.
Combine data sources using Or().
namespace DynamicData.Samplz.Examples
{
public class CombineUsingOrViewModel
{
public CombineUsingOrViewModel()
{
//maintain databaseDevices + databaseDevices anytime
var databaseDevices = new SourceCache<IDevice,int>(device => device.Id);
var localDevices = new SourceCache<IDevice, int>(device => device.Id);
//combine both datasoures.
//Exclude updates if the current device is a DatabaseDevice and the previous is LocalDevice
//[ensures LocalDevice gets precedent]
//Alternatively, could use the same device object which has an extra Enum property for DeviceSource and adjust IgnoreUpdate lambda acordingly
var combinedDevices = databaseDevices.Connect()
.Or(localDevices.Connect())
.IgnoreUpdateWhen((current, previous) => current is DatabaseDevice && previous is LocalDevice)
.AsObservableCache();
//Do normal dd stuff like binding, or expose combinedDevices as a new data source
}
}
public interface IDevice
{
int Id { get; }
string Name { get; }
}
public class DatabaseDevice: IDevice
{
public int Id { get; }
public string Name { get; }
public DatabaseDevice(int id, string name)
{
Id = id;
Name = name;
}
}
public class LocalDevice : IDevice
{
public int Id { get; }
public string Name { get; }
public LocalDevice(int id, string name)
{
Id = id;
Name = name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment