Skip to content

Instantly share code, notes, and snippets.

View RolandPheasant's full-sized avatar
💭
In the mountains for a week,

Roland Pheasant RolandPheasant

💭
In the mountains for a week,
View GitHub Profile
//This is the mutable version
class StudentSummaryMutable
{
public int Id { get; private set; }
public string Name { get; private set; }
public IObservableCache<StudentWithClass, int> Classes { get; private set; }
public int[] Grades { get; private set; }
@RolandPheasant
RolandPheasant / Extension.cs
Last active November 23, 2016 20:42
Group on property which implements INotifyPropertyChanged
/// <summary>
/// Groups the source using the property specified by the property selector. Groups are re-applied when the property value changed.
///
/// When there are likely to be a large number of property changes specify a throttle to improve performance
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TGroupKey">The type of the group key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="propertySelector">The property selector used to group the items</param>
@RolandPheasant
RolandPheasant / gist:91cb2d3cb685eab49a937c1afbd7d60e
Created October 26, 2016 09:10
Populate JobInfoRepository observable data
public class JobInfoRepository : IDisposable
{
private readonly IDisposable _cleanUp;
public IObservableCache<JobInfo, int> Results { get; }
public JobInfoRepository()
{
var readWriteCache = new SourceCache<JobInfo, int>(ji => ji.JobID);
Results = readWriteCache.AsObservableCache();
public static class SqlObservations
{
public static IObservable<IEnumerable<IDataRecord>> MonitorChanges(string connectionString, string sql)
{
/*
1. read data from database and return results
2. monitor changes
3. when there is a change, repeat the whole process
*/
public static clas DynamicDataJoinEx
{
/// <summary>
/// Joins the left and right observable data sources, combining the content into a single
/// </summary>
/// <typeparam name="TLeft">The object type of the left datasource</typeparam>
/// <typeparam name="TLeftKey">The key type of the left datasource</typeparam>
/// <typeparam name="TRight">The object type of the right datasource</typeparam>
/// <typeparam name="TRightKey">The key type of the right datasource</typeparam>
/// <typeparam name="TDestination">The resulting object which </typeparam>
/// <summary>
/// Transforms the without updates. Blah Blah
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <param name="source">The source.</param>
/// <param name="factory">The factory.</param>
/// <param name="updateAction">Apply changes to the original. Example (original, newitem) => original.Value = newitem.Value </param>
/// <returns></returns>
public static IObservable<IChangeSet<WidgetListItemViewModel, int>> TransformWithupdate(this IObservable<IChangeSet<Widget, int>> source )
{
return Observable.Create<IChangeSet<WidgetListItemViewModel, int>>(observer =>
{
var shared = source
.Transform(w => new WidgetListItemViewModel(w))
.Publish();
//create cache which never replaces the original problem
var nonUpdatingObservableCache = shared
public static class MyDynamicDataExtensions
{
public static IObservable<IChangeSet<WidgetListItemViewModel, int>> TransformWithupdate(this IObservable<IChangeSet<Widget, int>> source )
{
return Observable.Create<IChangeSet<WidgetListItemViewModel, int>>(observer =>
{
//create local data source wich tansforms all items
var localDataSource = source
.Transform(w => new WidgetListItemViewModel(w))
.AsObservableCache();
@RolandPheasant
RolandPheasant / CombineUsingOrViewModel
Last active July 11, 2016 20:11
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);
public static class DynamicDataExtensions
{
public static IObservable<IChangeSet<TObj, TKey>> FilterOnProperty<TObj, TKey, TProp>(this IObservable<IChangeSet<TObj, TKey>> source,
Expression<Func<TObj, TProp>> selectProp,
Func<TObj, bool> predicate) where TObj : INotifyPropertyChanged
{
return Observable.Create<IChangeSet<TObj, TKey>>(observer =>
{
//share the connection, otherwise the entire observable chain is duplicated