The rental service

The contracts and service operations that we have created to retrieve titles are pretty simple. They might be all you need in a real-life application. However, it is more likely that you will need data contracts that are more complex. To demonstrate this, we've added the rental service to the demo application. The rental service allows external applications to retrieve rental information or create rentals. Creating this service with all data contracts step-by-step would take too long, so we will discuss the artifacts only at a high level, starting with the database schema of the tables that we will use.

Rental header and line tables

The following is a simple schema of the tables that we will use. A rental header contains information about the rental, such as the store and the transaction date. A rental header is related to one or more lines that contain the details of the rental, such as the item that was rented:

Rental header and line tables

Rental service operations

There are three service operations available in the rental service:

  • CreateRental: This service operation takes a parameter of type CVRRentalDocumentDataContract and uses it to register a rental in the CVRRentalTable and CVRRentalLine tables
  • GetAllRentals: This service operation returns a list of CVRRentalDocumentDataContract data contracts by using the CVRRentalDocumentListDataContract data contract
  • GetAllRentalsForMember: This service operation does the same as the GetAllRentals operation but returns rentals only for a specific member

Rental data contracts

There are a total of five data contracts that the rental service uses. The relationships between these data contracts and the service operations are explained in the following diagram:

Rental data contracts

From the bottom up, the following are the contracts and their functions:

  • CVRRentalLineDataContract: This data contract contains the properties of a rental line, including the title and the return date.
  • CVRRentalLineListDataContract: This data contract contains a list of lines. It uses AifCollectionTypeAttribute to describe that the list contains items of the CVRRentalLineDataContract type.
  • CVRRentalHeaderDataContract: This data contract contains the header information about a rental, including the member ID and the transaction date.
  • CVRRentalDocumentDataContract: This data contract represents a rental document. It contains a header and a list of lines, respectively using the CVRRentalHeaderDataContract and CVRRentalLineListDataContract types.
  • CVRRentalDocumentListDataContract: This data contract contains a list of rental documents and is used in the getAllRentals and getAllRentalsForMember service operations.

This demonstrates that you can use data contracts within data contracts to make logical entities. Although it might seem complex at first glance, each class has its own responsibilities, which makes them reusable and easier to maintain.

The createRental service operation

The following is the createRental service operation. It uses the rental document data contract to register a rental in the database:

[SysEntryPointAttribute(true)]
public CVRRentalRefRecId createRental(CVRRentalDocumentDataContract _rentalDocument)
{
    CVRRentalTable rentalTable;
    CVRRentalLine  rentalLine;

    CVRRentalLineDataContract      lineDataContract;
    CVRRentalLineListDataContract  lineListDataContract;

    ListEnumerator  enumerator;

    // Insert the rental header
    rentalTable.clear();
    rentalTable.Id = _rentalDocument.parmHeaderContract().parmId();
    rentalTable.Member = CVRMember::find(_rentalDocument.parmHeaderContract().parmMemberId()).RecId;
    rentalTable.Shop = CVRShop::find(_rentalDocument.parmHeaderContract().parmShopId()).RecId;
    rentalTable.TransDate  = _rentalDocument.parmHeaderContract().parmTransDate();
    rentalTable.insert();

    // Get the list of rental lines
    lineListDataContract = _rentalDocument.parmLinesContract();

    // Initialize an enumerator to loop the lines
    enumerator = lineListDataContract.parmRentalLineList().getEnumerator();

    // As long as we have lines
    while(enumerator.moveNext())
    {
        // Get the current line
        lineDataContract = enumerator.current();

        rentalLine.clear();
        rentalLine.Rental = rentalTable.RecId;
        rentalLine.Title = CVRTitle::find(lineDataContract.parmTitleId()).RecId;
        rentalLine.ReturnDate  = lineDataContract.parmReturnDate();
        rentalLine.insert();
    }

    return rentalTable.RecId;
}

Now, let's see how we can consume the services that we have created using a .NET WCF application.

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

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