Following my previous post where I threw some ideas out about how to architect a remote WPF user interface, I have now had the chance to flesh out more of the details. I will attempt to dig a little deeper into the implementation without getting bogged down with the details in this post.
.NET Remoting is one solution to the general problem of creating client-side proxy objects that transparently represent their server-side instances. It is however not an ideal solution for rhobust systems because of all the gotchas associated with the service interface boundaries not being explicit.
We dont need such a generalized or complex solution as .Net Remoting because we are dealing with only one family of classes that conform to a distict pattern. We are therefore able to limit the communication/event propagation protocol to support only the functionality required for remoting View-Model objects that a WPF view could bind to.
These are the limitations of View-Model classes that we will conform to:
- View-Model classes are data-only classes that implement INotifyPropertyChanged in order to inform the view when properties (or collections) change state.
- The only functional behaviour associated with these classes is achieved by providing properties that implement the ICommand interface.
- All instances of objects are instantiated by the model, never by the view
[ServiceContract]
public interface IProxyViewModelService
{
[OperationContract]
Instance GetViewModel(string sessionId);
[OperationContract]
List<ViewModelUpdate> GetUpdates(string sessionId);
[OperationContract]
void SendUpdates(string sessionId, List<PropertyUpdate> updates);
[OperationContract]
void SendCommand(string sessionId, CommandInvocation commandInvocation);
}
You will notice that the client will poll for updates using the GetUpdates() request. In my experience it is much simpler to implement and get the permissions and routing to work out of the box. With a little attention to implementation detail we can even get it to be just as responsive and bandwidth-savvy as a callback service. But that will have to be another post.
In this interface I have defined some classes that may need clarification:
Instance => The base class for all Server Instances that get sent over the wire.
ValueInstance => A value-type instance that holds an immutable object like a string or int
ObjectInstance =>A fully serialized object that exists on the server.
CollectionInstance => A fully serialized collection of either Value, or Object instances.
ReferenceInstance => Contains an Id that uniquely identifies each (object or collection) instance on both client and server.
PropertyUpdate => Holds the objectId of the owning object, propertyName, new and old Instance values.
CollectionUpdate => Holds the objectId of the owning collection, plus similar properties to the NotifyCollectionChangedEventArgs that WindowsBase provides.
ViewModelUpdate => the base class for all updates.
The server must be concerned with:
- Serializing the current state of the view model object so it can be re-created on the client
- Provide notification of property-updates and collection-modifications to all registered clients
- Applying updates to properties from the clients
- Keeping track of registrations
The client must be concerned with:
- Reconstructing view model objects from the serialized form as provided by the server
- Applying collection modifications and property updates as they are propagated from the server
- Notifying the server of changes to properties that the client makes
- Mocking ICommand interfaces to invoke service calls
I must explain that although the ViewModel class should conform strictly to the requirements I specified at the beginning of the article, there is one more requirement that I must mention. It must have a parameterless constructor so that the proxy object can be created on the client side. This poses a problem: Often our server side objects have dependencies (on database repositories and the like) that should be passed in on the constructor. To support this scenario there needs to be some mechanism for the implementation of the View Model to inherit from a base class. It is the base class that defines the interface that the View requires, and it is the base class that will be de-serialized on the client.
We can see from the diagram above that the view can be programmed independently of whether it is a remote interface or not, except for the knowledge about whether the session/connection is still alive. A small consideration when trying to provide a WPF UI to a Windows Service by any other means!
