[Feature]: Add TransformVirtual Extensions for IObservableList and IObservableCache

オープン
#1,174 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
47/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
csharp
領域
backend

調査の方向性

Read the existing Transform operator and the IObservableList/IObservableCache interfaces, then inspect how Connect and Preview expose change sets. Implementations are shown for IObservableList, but the issue does not identify repository files or tests, and the cache behavior still needs working out. Done means both extensions expose virtual transformed views with the described access, subscription, and disposal behavior; add tests for list and cache behavior.

索引モデルが issue の本文から書いたものです。

説明

Describe the functionality desired 🐞

New extension methods TransformVirtual<TSource, TResult>(Func<TSource, TResult> transformFactory) for IObservableList<TSource> and IObservableCache<TSource, TKey> that create lightweight views of type IObservableList<TResult>/IObservableCache<TResult, TKey> without intermediate storage collection.

There's an implementation for IObservableList I created and used in my project:

public static class CollectionExtensions
{
    extension<T>(IReadOnlyList<T> source)
    {
        public IReadOnlyList<TResult> TransformVirtual<TResult>(Func<T, TResult> transformFactory)
        {
            ArgumentNullException.ThrowIfNull(source);
            ArgumentNullException.ThrowIfNull(transformFactory);

            return new ReadOnlyListTransformVirtual<T, TResult>(source, transformFactory);
        }
    }

    private sealed class ReadOnlyListTransformVirtual<TSource, TResult> : IReadOnlyList<TResult>
    {
        private readonly IReadOnlyList<TSource> _source;
        private readonly Func<TSource, TResult> _transformFactory;

        public ReadOnlyListTransformVirtual(IReadOnlyList<TSource> source, Func<TSource, TResult> transformFactory)
        {
            _source = source;
            _transformFactory = transformFactory;
        }

        public TResult this[int index] => _transformFactory(_source[index]);

        public int Count => _source.Count;

        public IEnumerator<TResult> GetEnumerator() => _source.Select(x => _transformFactory(x)).GetEnumerator();

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    }
}

public static class ObservableListExtensions
{
    extension<T>(IObservableList<T> source) where T : notnull
    {
        public IObservableList<TResult> TransformVirtual<TResult>(Func<T, TResult> transformFactory) where TResult : notnull
        {
            ArgumentNullException.ThrowIfNull(source);
            ArgumentNullException.ThrowIfNull(transformFactory);

            return new ObservableListTransformVirtual<T, TResult>(source, transformFactory);
        }
    }

    private sealed class ObservableListTransformVirtual<TSource, TResult> : IObservableList<TResult>
        where TSource : notnull
        where TResult : notnull
    {
        private readonly IObservableList<TSource> _source;
        private readonly Func<TSource, TResult> _transformFactory;

        public ObservableListTransformVirtual(IObservableList<TSource> source, Func<TSource, TResult> transformFactory)
        {
            _source = source;
            _transformFactory = transformFactory;
        }

        public int Count => _source.Count;

        public IObservable<int> CountChanged => _source.CountChanged;

        public IReadOnlyList<TResult> Items => _source.Items.TransformVirtual(_transformFactory);

        public void Dispose()
        {
        }

        public IObservable<IChangeSet<TResult>> Connect(Func<TResult, bool>? predicate = null)
        {
            var ret = _source.Connect().Transform(_transformFactory);
            if (predicate is not null)
            {
                ret = ret.Filter(predicate);
            }
            return ret;
        }

        public IObservable<IChangeSet<TResult>> Preview(Func<TResult, bool>? predicate = null)
        {
            var ret = _source.Preview().Transform(_transformFactory);
            if (predicate is not null)
            {
                ret = ret.Filter(predicate);
            }
            return ret;
        }
    }
}

Sincerely thank all of you for creating DynamicData!

The steps the functionality will provide

From the end-user perspective:

  1. Call the new extension method TransformVirtual on any IObservableList/IObservableCache to create another stateless IObservableList/IObservableCache as a projected view without intermediate storage collection in a fluent way.
  2. Transformation execution is on-demand and should be pure (no side effects). The transformFactory will run if items are accessed via indexer or enumeration. There is no precomputation or materialization, unless a subscription of a downstream observer via Connect or Preview occurs.
  3. Disposal of the returned IObservableList/IObservableCache is not required and does nothing because it's just a stateless wrapper.
  4. Fully compatible with consumer scenarios that require direct index access, count property and enumeration support.
Considerations

Currently, in order to transform the elements of an IObservableList/IObservableCache, the Connect method needs to be called first and then use the Transform operator of observable change set. After that, the type of the current operating object becomes observable change set and the direct accessors of collection count and items are lost. In some cases, SourceList/SourceCaches are internally used and some transformed IObservableList/IObservableCaches should be exposed as public API. To revert to IObservableList/IObservableCache type, the method AsObservableList/AsObservableCache can be used, which creates intermediate storage collection (e.g. SourceList is created for IObservableList). If we just want to do a simple transformation, such as a type cast (even the implicit cast because covariance is not supported by DynamicData), this method is inefficient and unnecessary. To do this in a better way, we can just create a stateless wrapper of IObservableList/IObservableCache without inherent subscription and collection allocation.

主要言語
C#
スター
1.9k
フォーク
194
平均マージ
12分
マージ済み PR(30日)
1

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

reactivemarbles/DynamicData のほかの issue

reactivemarbles/DynamicData の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。