Reputation: 41
We have some WPF/Silverlight controls we have written using the traditional event architecture (no commands etc.), and want to convert it to MVVM compliant ones. I researched on this subject, and i take it i will have to write commands to wrap the events that i currently have in the control. I would like to design it correctly now so i want to also make it RX complaint, rather than just commands.
Can someone explain to me how i can design my currently designed events into commands and RX ones please.
It will be useful if we took something like a control's Click event, and design command for it, and also RX compliant (observable, etc.), so i can understand what is involved.
Currently it is simple events, its pretty simple to understand to work with it :)... so one can subscribe to it and execute custom handlers. My employer wants me to make this commands and also RX-ify it.
I read some articles on these, but found it to require some PhD degree to understand (mathematical Dual, Subject<T>, etc.) :) I could not get my heads around it. I am struggling to taking this and making it into commands and also into RX. Please help. I want to learn this properly, so i don't have to re-design it again once more.
Thanks in advance.
Upvotes: 4
Views: 949
Reputation: 10783
Paul D, I think you may be just forcing technology for technology sake.
First you want to use the Command pattern in WPF/SL so that you can write testable code (MVVM). This is independent of Rx. If you have your ViewModel constructed correctly then if you choose to use Rx somewhere else (Repository, Model, Controllers etc) then great.
The reason that WPF uses commands instead of the Event pattern is because a command tells something to happen, where as an event broadcasts that something did happen. When you write a Control it is fine for you to broadcast that a Click occured, but in you ViewModel a command reflects that something should happen.
If we look at an example:
a SubmitOrderCommand may initiate a call to submit the state of the Model to a repository. This in itself does not need to involve Rx. The progress of the Submission however, may utilise Rx to publish the state changes eg Submitting-->Submitted-->CreditChecked-->Accepted.
This may then result in a some contracts that look like this (psuedo code)
class OrderEntryViewModel
{
public ICommand SubmitOrderCommand { get; }
}
interface IOrderRepository
{
IObservable<OrderStatus> SubmitOrder(SubmitOrderRequest request);
}
I agree with you that there is way too much Academia around Rx. I recomment that you check out my blog introduction to Rx. I hope it helps you grasp Rx much faster.
UPDATE: Check out my book IntroToRx.com instead. It replaces the blog series.
Upvotes: 2
Reputation: 924
I have no idea about ReactiveXaml but if you want to bind events to command, I could advise you MVVM light. You can try something similar to the code below, moreover MVVM light is provided with an EventToCommand Sample:
<Window
...
xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
/>
<ComboBox ItemSource="{Binding MyCollection}">
<Interactivity:Interaction.Triggers>
<Interactivity:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding Path=LoadCommand}" />
</Interactivity:EventTrigger>
</Interactivity:Interaction.Triggers>
</ComboBox>
Upvotes: 0