Creating the service interface layer

The last step is to create the service interface layer. Again, the steps here are very similar to the steps in Chapter 4, Implementing a WCF Service in the Real World, so you can refer to that chapter for more details.

  1. Right-click on the solution item and select Add | New Project.... Add a WCF service library project with the name of LINQNorthwindService.
  2. Add a project reference to LINQNorthwindLogic and LINQNorthwindBDO to this new service interface project.
  3. Change the service interface file IService1.cs, as follows:

    a. Change its filename from IService1.cs to IProductService.cs.

    b. Change the interface name from IService1 to IProductService, if it is not done for you.

    c. Remove the original two service operations and add the following two new operations:

    [OperationContract]
    [FaultContract(typeof(ProductFault))]
    Product GetProduct(int id);
    
    [OperationContract]
    [FaultContract(typeof(ProductFault))]
    bool UpdateProduct(ref Product product, ref string message);

    d. Remove the original CompositeType and add the following data contract classes:

    [DataContract]
    public class Product
    {
       [DataMember]
        public int ProductID { get; set; }
        [DataMember]
        public string ProductName { get; set; }
        [DataMember]
        public string QuantityPerUnit { get; set; }
        [DataMember]
        public decimal UnitPrice { get; set; }
        [DataMember]
        public bool Discontinued { get; set; }
        [DataMember]
        public byte[] RowVersion { get; set; }
    }
    
    [DataContract]
    public class ProductFault
    {
        public ProductFault(string msg)
        {
            FaultMessage = msg;
        }
    
        [DataMember]
        public string FaultMessage;
    }

    e. The following is the content of the IProductService.cs file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    
    namespace LINQNorthwindService
    {
        [ServiceContract]
        public interface IProductService
        {
            [OperationContract]
            [FaultContract(typeof(ProductFault))]
            Product GetProduct(int id);
    
            [OperationContract]
            [FaultContract(typeof(ProductFault))]
            bool UpdateProduct(ref Product product, 
                ref string message);
        }
    
        [DataContract]
        public class Product
        {
            [DataMember]
            public int ProductID { get; set; }
            [DataMember]
            public string ProductName { get; set; }
            [DataMember]
            public string QuantityPerUnit { get; set; }
            [DataMember]
            public decimal UnitPrice { get; set; }
            [DataMember]
            public bool Discontinued { get; set; }
            [DataMember]
            public byte[] RowVersion { get; set; }
        }
        [DataContract]
        public class ProductFault
        {
            public ProductFault(string msg)
            {
                FaultMessage = msg;
            }
    
            [DataMember]
            public string FaultMessage;
        }
    }
  4. Change the service implementation file Service1.cs, as follows:

    a. Change its filename from Service1.cs to ProductService.cs.

    b. Change its class name from Service1 to ProductService, if it is not done for you.

    c. Add the following two using statements to the ProductService.cs file:

    using LINQNorthwindLogic;
    using LINQNorthwindBDO;

    d. Add the following class member variable:

    ProductLogic productLogic = new ProductLogic();

    e. Remove the original two methods and add following two methods:

    public Product GetProduct(int id)
    {
        ProductBDO productBDO = null;
        try
        {
            productBDO = productLogic.GetProduct(id);
        }
        catch (Exception e)
        {
            string msg = e.Message;
            string reason = "GetProduct Exception";
            throw new FaultException<ProductFault>
                (new ProductFault(msg), reason);
        }
    
        if (productBDO == null)
        {
            string msg =
                string.Format("No product found for id {0}",
                id);
            string reason = "GetProduct Empty Product";
            throw new FaultException<ProductFault>
                (new ProductFault(msg), reason);
        }
        Product product = new Product();
        TranslateProductBDOToProductDTO(productBDO, product);
        return product;
    }
    
    public bool UpdateProduct(ref Product product,
        ref string message)
    {
        bool result = true;
    
        // first check to see if it is a valid price
        if (product.UnitPrice <= 0)
        {
            message = "Price cannot be <= 0";
            result = false;
        }
        // ProductName can't be empty
        else if (string.IsNullOrEmpty(product.ProductName))
        {
            message = "Product name cannot be empty";
            result = false;
        }
        // QuantityPerUnit can't be empty
        else if (string.IsNullOrEmpty(product.QuantityPerUnit))
        {
            message = "Quantity cannot be empty";
            result = false;
        }
        else
        {
            try
            {
                var productBDO = new ProductBDO();
                TranslateProductDTOToProductBDO(product, productBDO);
                result = productLogic.UpdateProduct(
                    ref productBDO, ref message);
                 product.RowVersion = productBDO.RowVersion;
            }
            catch (Exception e)
            {
                string msg = e.Message;
                throw new FaultException<ProductFault>
                    (new ProductFault(msg), msg);
            }
        }
        return result;
    }
  5. Because we have to convert between the data contract objects and the business domain objects, we need to add the following two methods:
    private void TranslateProductBDOToProductDTO(
        ProductBDO productBDO,
        Product product)
    {
        product.ProductID = productBDO.ProductID;
        product.ProductName = productBDO.ProductName;
        product.QuantityPerUnit = productBDO.QuantityPerUnit;
        product.UnitPrice = productBDO.UnitPrice;
        product.Discontinued = productBDO.Discontinued;
        product.RowVersion = productBDO.RowVersion;
    }
    
    private void TranslateProductDTOToProductBDO(
        Product product,
        ProductBDO productBDO)
    {
        productBDO.ProductID = product.ProductID;
        productBDO.ProductName = product.ProductName;
        productBDO.QuantityPerUnit = product.QuantityPerUnit;
        productBDO.UnitPrice = product.UnitPrice;
        productBDO.Discontinued = product.Discontinued;
        productBDO.RowVersion = product.RowVersion;
    }
  6. Change the config file App.config, as follows:

    a. Change Service1 to ProductService.

    b. Remove the word Design_Time_Addresses.

    c. Change the port to 8080.

    d. Now, BaseAddress should be as follows:

    http://localhost:8080/LINQNorthwindService/ProductService/

    e. Copy the connection string from the App.config file in the LINQNorthwindDAL project to the following App.config file:

      <connectionStrings>
        <add name="NorthwindEntities" 
    connectionString="metadata=res://*/Northwind.csdl|res://*/Northwind.ssdl|res://*/Northwind.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=Northwind;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>

    Note

    You should leave the original connection string untouched in the App.config file in the data access layer project. This connection string is used by the Entity Model Designer at design time. It is not used at all during runtime, but if you remove it, whenever you open the entity model designer in Visual Studio, you will be prompted to specify a connection to your database.

Now build the solution and there should be no errors.

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

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