[Feature]: Add TransformVirtual Extensions for IObservableList and IObservableCache

Đang mở
#1,174 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
47/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
backend

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C#
Star
1.9k
Fork
194
Merge trung bình
12 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của reactivemarbles/DynamicData

Tất cả issue của reactivemarbles/DynamicData

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.