Overview of the Sample Bookshop WCF Service

,

The Bookshop demo application includes a server-side component, which is used by both the ProductsViewModel and ProductDetailsViewModel classes, providing the application with a set of products to display. The server-side component is fairly arbitrary and is presented here merely for the sake of completeness.

The WCF service is called BookshopService and resides in the WPUnleashed.Web project of the downloadable sample code (see Listing 3.15).

LISTING 3.15. BookshopService Class


[AspNetCompatibilityRequirements(
    RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class BookshopService : IBookshopService
{
    public IEnumerable<Product> GetProducts()
    {
        return ProductManager.Products;
    }

    public Product GetProductById(int productId)
    {
        return ProductManager.GetProductById(productId);
    }
}


The BookshopService exposes static methods of the ProductManager class, shown in Listing 3.16. The ProductManager class creates an XDocument instance, using an XML file, to populate a list of Products.

LISTING 3.16. ProductManager Class


public static class ProductManager
{
    static readonly List<Product> products = new List<Product>();

    public static IEnumerable<Product> Products
    {
        get
        {
            return products;
        }
    }

    static ProductManager()
    {
        string path = HttpContext.Current.Server.MapPath(
            "~/Services/Bookshop/Products.xml");
        XDocument document = XDocument.Load(path);
        foreach (XElement element in
            document.Element("Products").Elements("Product"))
        {
            var product = new Product(element);
            product.SmallImageUri
                = ServerUtility.ResolveServerUrl(product.SmallImageUri);
            product.LargeImageUri
                = ServerUtility.ResolveServerUrl(product.LargeImageUri);
            products.Add(product);
        }
    }

    public static Product GetProductById(int id)
    {
        if (id < 0 || id > products.Count)
        {
            throw new ArgumentOutOfRangeException("id");
        }
        return products[id - 1];
    }
}


The Product class contains the properties that are used to display each book’s details, such as Title and Author. The Product class also knows how to populate itself from an XElement. The explicit casting operators of the XElement class make it easy to extract the values to the Product properties, as shown in the following excerpt from the Product class:

public Product(XElement element)
{
    if (element == null)
    {
        throw new ArgumentNullException("element");
    }
    Id = (int)element.Element("Id");
    Title = (string)element.Element("Title");
    Author = (string)element.Element("Author");
    Description = (string)element.Element("Description");
    SmallImageUri = (string)element.Element("SmallImageUri");
    LargeImageUri = (string)element.Element("LargeImageUri");
    Price = (double)element.Element("Price");
    Isbn10 = (string)element.Element("ISBN-10");
    Isbn13 = (string)element.Element("ISBN-13");
    ExternalUrl = (string)element.Element("ExternalUrl");
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.46.96