简体   繁体   中英

Rx: Create observable without subject

Sorry for the title - it's an oversimplification but I didn't know what else to title this.

I'm quite new to Rx and have the following scenario I'm trying to solve.

Items come from the server with a collection of vendors. One of those vendors will be nominated as the primary. To make them editable/bindable in WPF I have created some proxy reactive objects that will "react" to changes in the UI and recalculate some numbers.

In order to make sure only one vendor can be the primary I have implemented the code below - which uses a subject to get the work done.

I've read subjects can be a code smell and I'd like to know if what I've done is "the right way" or if there's a cleaner approach that could be taken.

public class VendorDto
{
    public string Name { get; set; }
    public bool IsPrimary { get; set; }
}

public class VendorProxy : ReactiveObject
{
    // INPC for Name and other properties. 

    public bool IsPrimary => _isPrimary.Value;
    private readonly ObservableAsPropertyHelper<bool> _isPrimary;

    public ReactiveCommand<Unit, Unit> MakePrimary { get; }

    public VendorProxy(VendorDto dto, IObservable<VendorProxy> primaryVendors, Action<VendorProxy> makePrimary)
    {
        primaryVendors
            .DistinctUntilChanged()
            .Select(x => x == this)
            .ToProperty(this, x => x.IsPrimary, out _isPrimary);

        MakePrimary = ReactiveCommand.Create(() => makePrimary(this), 
            canExecute: this.WhenAnyValue(x => x.IsPrimary, alreadyPrimary => !alreadyPrimary));

        if (dto.IsPrimary) 
            makePrimary(this); // set the initial value of IsPrimary. 
    }

    // implements IEquality.
}

public class ItemProxy : ReactiveObject
{
    private ISubject<VendorProxy> PrimaryVendorSubject { get; } = new BehaviorSubject<VendorProxy>(null);

    public IObservable<VendorProxy> PrimaryVendorChanged => PrimaryVendorSubject;

    public ReactiveList<VendorProxy> Vendors { get; }

    public VendorProxy PrimaryVendor => _primaryVendor.Value;
    private readonly ObservableAsPropertyHelper<VendorProxy> _primaryVendor;

    public Item()
    {
        // these come from a web service.
        var dtos = new[] {
            new Vendor {Name = "Vendor A", IsPrimary = true}, 
            new Vendor {Name = "Vendor B", IsPrimary = false}
        }

        Vendors = new ReactiveList<VendorProxy>(dtos.Select(dto => 
            new VendorProxy(dto, PrimaryVendorChanged, PrimaryVendorSubject.OnNext)));

        // some other subscriptions that require the primary vendor to do their work. 
    }
}

I can't really test this, though it looks right. As for cleaner, that's a bit in the eye of the beholder. To me it looks more reactive.

I removed the subject from ItemProxy and the 'callback' action from VendorProxy . ReactiveCommands are observables, and ReactiveList can get turned into an observable, so all of that is derivable (in the primaryStream variable) in the ItemProxy constructor.

public class VendorProxy : ReactiveObject
{
    // INPC for Name and other properties. 

    public bool IsPrimary => _isPrimary.Value;
    private readonly ObservableAsPropertyHelper<bool> _isPrimary;

    public ReactiveCommand<Unit, Unit> MakePrimary => ReactiveCommand.Create(() => Unit.Default, 
        canExecute: this.WhenAnyValue(x => x.IsPrimary, alreadyPrimary => !alreadyPrimary));

    public VendorProxy(VendorDto dto, IObservable<VendorProxy> primaryVendors)
    {
        primaryVendors
            .DistinctUntilChanged()
            .Select(x => x == this)
            .ToProperty(this, x => x.IsPrimary, out _isPrimary);

        if(dto.IsPrimary)
            MakePrimary.Execute();
    }

    // implements IEquality.
}

public class ItemProxy : ReactiveObject
{
    public ReactiveList<VendorProxy> Vendors { get; }

    public VendorProxy PrimaryVendor => _primaryVendor.Value;
    private readonly ObservableAsPropertyHelper<VendorProxy> _primaryVendor;

    public ItemProxy()
    {
        // these come from a web service.
        var dtos = new[] {
            new VendorDto {Name = "Vendor A", IsPrimary = true},
            new VendorDto {Name = "Vendor B", IsPrimary = false}
        };

        Vendors = new ReactiveList<VendorProxy>();

        var primaryStream = Vendors.ItemsAdded.Select(_ => Unit.Default)
            .Merge(Vendors.ItemsRemoved.Select(_ => Unit.Default))
            .Select(_ => Observable.Merge(Vendors.Select(v => v.MakePrimary.Select(__ => v))))
            .Switch();

        primaryStream
            .DistinctUntilChanged()
            .ToProperty(this, x => x.PrimaryVendor, out _primaryVendor);

        var proxies = dtos
            .Select(dto => new VendorProxy(dto, primaryStream));

        Vendors.AddRange(proxies);

        // some other subscriptions that require the primary vendor to do their work. 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM