对不起标题 - 它过于简单了,但我不知道还有什么标题。
我对Rx很陌生并且有以下情况我试图解决。
项目来自服务器,其中包含一系列供应商。其中一家供应商将被提名为主要供应商。为了使它们在WPF中可编辑/可绑定,我创建了一些代理反应对象,这些对象将会反应出来。更改UI并重新计算一些数字。
为了确保只有一个供应商可以成为主要供应商,我已经实施了以下代码 - 它使用主题来完成工作。
我已经阅读了可以成为代码气味的主题,并且我想知道我所做的是"正确的方式"或者如果有更清洁的方法可以采取。
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.
}
}
答案 0 :(得分:0)
虽然看起来不对,但我无法对此进行测试。至于清洁工,这一点在旁观者的眼中。对我来说,它看起来更具反应性。
我从ItemProxy
和&#39;回调&#39;中移除了主题。来自VendorProxy
的行动。 ReactiveCommands
是可观察的,ReactiveList
可以变为可观察的,因此所有这些都可以在primaryStream
构造函数中派生(在ItemProxy
变量中)。
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.
}
}