One of the first things people tend to do when there are building an application (at least I do!), is try to identify the models (POCO’s) which will be used throughout the application. Because we added a service refence to WCF service provided by Micorsoft, a base for the models are already provided. I created a class diagram to give some more insight on the way the models are constructed.
This class diagram shows us a couple of things. First of all, we have a base class which serves as a base for all the possible tag types. A more important observation is that this base class implements the IExtensibleDataObject interface (http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iextensibledataobject.aspx ). The implementation of this interface ensures us that the properties marked with the DataContractAttribute attribute are preserved while serializing and deserializing. Even better, when serialized to a parent or child class the data is preserved in the ExtentionData property provided by the interface.
At this moment we have successfully identified the main part of the models provided by the API. It is now time to make sure we can take advantage of these models and their behavior. In some cases you can use the models as-is and you can build your software on top of the base classes provided but more often you’ll need some additional properties on the model to make it usefull for this specific environment.
For this post I’ll extend the base classes with a minimal amount of properties, so we can use these properties in Part 3 of this multi-post. I usually work a lot with interfaces and abstracts to minimize the amount of duplicate code and minimize the risk of built-in dependencies. In this case I’ll introduce a couple of interfaces and an extension method to work with the IExtensibleDataObject interface.
As you can see in this class diagram, all extended objects implement an interface which inherits from ITag and they inherit from the classes provided by the API. The ITag interface inherits from the IExtensibleDataObject in order to accommodate the extension method. The concrete (and simple!) implementation of UriTagExtended looks like this:
The next step is to implement an extension method which allows us to go back and forth to the base object and our extendedobject. The extension method hooks in on the IExtensibleDataObject interface and equips every class inheriting from that interface with an additional method: ConvertTo<T>. This extension serializes the object and deserializes it into T (and store any additional info in the DataExtension property). Our extension method is implemented as follows:
With the implementation of our extension method we are able to revert our extended objects back to the base objects that are supplied to the methods of the API. In the next part of this multi-part blog post I will wrap the API in a repository pattern and make some more use of the extended properties and the extension method we created.