Data Contract and Data Member Attributes Complete the Service Contract

As I stated several times in recent posts, the purpose of the Art Service is not to expose the business logic of the World of Art web app. Instead, the purpose of the Art Service, is to simply expose specific data elements from the World of Art domain, which can be served upon request, to various software clients. We have already discussed in detail, how the service contract attribute and the operation contract attribute, transform a simple interface and class implementation, into an actual web service. Now lets look at how we can complete the details of the service contract, by specifying exactly which data elements are to be exposed by the web service.

In the early phases of development, I isolated the entities of the world of art domain into a separate class library called ArtWorldObjects. Artist is one of the entities defined in this class library. At this time, I want the WorldOfArtServiceApp to only expose the Artist object, and more specifically, only certain properties of the Artist object. To acomplish this, I will utilize the Data Contract Attribute Class and the Data Member Attribute Class. The data contract attribute specifies that the type defines or implements a data contract. The data member contract, when applied to the member of a type, specifies that the member is part of a data contract, and can be serialized by the DataContractSerializer Class.

Here is the source code for the IArtist interface and the Artist class, with data contract attribute and data member attribute applied;

Click on this link; Artists in JSON Format to send a “GET” request to the World of Art Web Service app. You can also copy the following link into your browser’s address bar to retrieve the JSON data;

Upon examining the JSON text returned by the service, you will see that the only properties of artist returned by the web service, are those enhanced with the Data Member attribute. Here is what the artist data in JSON format looks like;

For the sake of convenience I added three properties, of type string, to the IArtist Interface;

  • ExceptionMessage – messages to communicate any problems to the client
  • DateOfBirthShortString – date in “mm/dd/yyyy” format
  • DateOfBirthLongString – date in fomat “Monday, August 06, 1928″

The short and long data formats where necessary, because I discovered that when type System.DateTime is serialized to JSON, a very nasty date format is produced. Something like this; “DateOfBirth”:”\/Date(-296931600000-0700)\/”. Therefore, I excluded the date of birth property with a type of System.DateTime from being a Data Member. However, you can see in the source code, that the short and long date of birth properties are derived from the System.DateTime date of birth property.

In my next post, I will cover the configuration and deployment of the World Of Art Web Service. After that, I will cover the client, which uses JavaScript to consume artist data from the web service. This link; Artists from the Web Service will launch the client in the World of Art Web App.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">