I would like to still take advantage of WPF and the view-model/model-view pattern for my remote interface. I need some kind of proxy object that is instantiated by the service and is duplicated on the UI process.
The UI needs to be able to show updates that have occurred on the service, and the service needs to be able to respond to updates from the UI.
So I've been thinking about how we can do that in a way that is fairly transparent to both the UI and service. I still want the UI to use data-binding, and the ICommand interface for invoking actions. I want to be able to share one well defined view-model between both the service and UI, and not have to do any over-the-wire specific logic in either the service or UI.
The goal is to minimize any work related to making the UI remote. Creating the UI and presenter/view-model should be almost as easy as working with a view-model in the same process. I say "almost" because it there are specific concerns related to working remotely that must be considered, but that concern should NOT overlap with the concern of the view-model/model-view pattern.
The question is "how can I distil this concern into a framework or set of base classes so it is no longer cross-cutting"
Option 1: The Remoting approach
With careful fine-tuning of lease parameters, you should be able to create a fairly reliable view-model derived from MarshalByRefObject, complete with two-way update notifications. The beauty of Remoting is that the proxy objects are all handled for you.
The problem is that because it is sooo transparent, a communication exception can happen just about on any call on the proxy object. Its very difficult to handle the exceptions, and even more difficult knowing how to recover. You also don't have any control of how chatty the events get. Configuring firewalls can be a problem too.
If the UI is only used for short periods of time, and the occasional restart-because-of-an-exception is bearable, Remoting may be an acceptable solution.
Option 2: The WCF approach
There are several hurdles to overcome:
Two-way communication.
This is needed to send notifications both ways. Subscribing to a callback service or event service is one option, but if you have ever tried to use this technique you will find it an absolute PITA to get working reliably. The other option is to use polling. With polling the question still remains: How often do I poll to make sure I:
- keep the user interface responsive, and
- not waste cpu time and network bandwidth when there is nothing to update.
I would suggest a polling strategy that removes this concern. The client polls as quickly as it is able to, but the server will block for a pre-arranged time if there are no messages. At any point during that timeout period a message arrives for delivery, the server will send the response immediately at that time. In this way all events that occur on the server will be delivered to the client instantaneously, even though polls are only occurring every 10 seconds!
How to create the proxy view-model
When it comes to creating a proxy object that reflects the state of the "real" object that exists on the server, it is always easier to proxy an interface. At least the top-level DataContext should be an interface, and then it would be helpful if everything the view-model referenced in it's interface was either another interface, or at least be serializable. These are pretty much the same constraints as the Remoting scenario, except using interfaces instead of MarshalByRefObject. Although the Wpf xaml engine does not require a well-defined interface (I mean interface in the broadest sense of the word), we will have to put in a little more work to make sure only what is important to the view gets duplicated over the wire.
Here is where I do something you may not be expecting. The view-model interface will NOT be the service interface.
I propose that the service interface be a generic and completely reusable interface that contains methods for replicating the viewModel over the wire, for notifying of property/collection changes, and for invoking commands.
We create a completely generic reusable service oriented library that is able to, by reflection
- Build up a tree of service objects that perfectly describes the state of the entire view-model so that it can be transferred over the wire and replicated at the UI end.
- Identify each object by a key (it's path in the tree?) so both server and client can notify of changes to that object
- Watch every watchable object in the view-model and queue the state changes so it can be picked up when the UI polls.
- Keep track of who the live clients are, with timeouts/ max queue size.
And from the UI (client) end:
- Dynamically construct a view-model from the tree it gets from the server upon registration.
- Transfer method calls on view-model interfaces into an equivalent service call to be invoked on the "real" model ( e.g. SaveCommand , RoutedCommand)
- Translate property changes and communicate them to the server as{objectId,propertyName,newValue}
- Poll for property changes (in background thread), and apply them to the replicated-model (in the dispatcher thread)
The UI will be aware of the remote nature of the view-model to the extent that it should be able to respond to the server being unavailable, in which case it must re-obtain the entire view-model upon successful reconnection. This behaviour could also be in the library, easily implemented by using a base class, or helper method.
Using this library it should be possible to split existing WPF applications into UI and service with the minimal amount of effort (assuming there is already a good separation in the code between UI and business logic).
Example
Consider a log-viewing application where you view the latest 500 log messages of a service.
The view-model looks something like:
interface ILogViewModel
{
ObservableCollection LogMessages{get;}
}
Here is a typical sequence of events:
- The client will recieve a tree representation of the entire viewModel (with all collection items) when it registers itself. It might use it's IP address as it's registration id.
- The client will poll (using it's registration id), and the server will block and wait for something to happen to the viewModel (namely the LogMessages collection getting modified)
- As soon as a log message comes in the server will reply with a status update (in this case it is a collectionChange{item inserted at 0} for the object identified "ILogViewModel\LogMessages")
- The client inserts the LogMessage according to the update it received
- The Wpf binding ensures that the view is updated also.
Tidy.