Customizing the product service

Now that we have finished customizing the data access layer and business logic layer, the last step of the service implementation is to customize the ProductService.cs file. If you open it now, you will find that it contains an empty method, GetProduct. We will customize it to call the business logic layer to retrieve a product, and throw a FaultException if no product is found.

We can also open the ProductService.cs file and modify it directly, adding the required functionality for the method GetProduct. However, because this class is generated by Service Factory, any change to it will be lost if we ever need to regenerate it. For example, we may want to add a new operation to the service in the future, which would necessitate it.

Just as we did for the ProductFault class, we will add another file, called ProductService.cs, but we will make it a partial class, to extend the generated ProductService class.

Follow these steps to add the partial class:

  1. In the Solution Explorer, right-click on the project MyWCF.EasyNorthwind.ServiceImplementation.
  2. Select Add | Class from the context menu.
  3. Add a new class file named ProductService.cs.

Then, customize this file as follows:

  1. Change it to make it a public partial class.
  2. Add six using statements for the BusinessEntities, DataContracts, MessageContracts, BusinessLogic, FaultContracts, and ServiceModel namespaces.
  3. Add a method, GetProduct, to call the business logic layer to retrieve details for a product, convert the product entity to a product contract, and then return it as a message.

The content of ProductService.cs should look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyWCF.EasyNorthwind.BusinessEntities;
using MyWCF.EasyNorthwind.DataContracts;
using MyWCF.EasyNorthwind.MessageContracts;
using MyWCF.EasyNorthwind.BusinessLogic;
using MyWCF.EasyNorthwind.FaultContracts;
using System.ServiceModel;
namespace MyWCF.EasyNorthwind.ServiceImplementation
{
public partial class ProductService
{
ProductLogic productLogic = new ProductLogic();
public override GetProductResponse GetProduct( GetProductRequest request)
{
// call business entity layer to retrieve a product
ProductEntity productEntity = productLogic.GetProduct(request.ProductID);
// throw a Fault if no product found
if (productEntity == null)
throw new FaultException<ProductFault>(new ProductFault("No product found with id " + request.ProductID), "Product Fault");
// translate it to a Product Data Contract object
Product product;
product = TranslateBetweenProductEntityAndProduct. TranslateProductEntityToProduct(productEntity);
// create a response message
GetProductResponse response = new GetProductResponse();
response.Product = product;
// return the response message
return response;
}
}
}

If you have used the previous version's Service Factory, you may recall that some of the tasks we put in the service implementation class were handled by a service adapter. This version's Service Factory doesn't create an adaptor by default. Instead, it puts all of them in the service implementation class. You can always create your own adaptor to provide an extra level of decoupling between the business logic and the service interface.

You can now build the ServiceImplementation project, to make sure that there is no build error.

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

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