Share via


Exercise 1: Exploring a Windows Forms Application

In this exercise you'll open an existing Visual Studio 2010 solution and walk through code found in a WCF and Windows Forms project. The main goal of the exercise is to get acquainted with an existing Windows Forms application and supporting code to better understand what code can be re-used during the migration to Silverlight. Throughout the exercise you'll view data access code that relies on Entity Framework 4, examine a WCF service contract and run the Windows Forms project to explore the functionality it offers. To get started, follow the steps below.

  1. Open Visual Studio 2010 and select File Open Project/Solution from the menu.
  2. Open the following Visual Studio solution file:

    Language

    [Lab Files Location]

    C#

    /MigratingToSilverlight/Starting Point/C#/CustomerViewer/CustomerViewer.sln

    Visual Basic

    /MigratingToSilverlight/Starting Point/VB/CustomerViewer/CustomerViewer.sln

  3. The following projects are available in the solution:

    • CustomerService.Model – Contains entities and data repository classes used to access an AdventureWorks LT database.
    • CustomersService – A WCF service application that exposes entities to various applications.
    • CustomerViewer – A Windows Forms project that consumes data from a WCF service.
    • CustomerViewer.Web – An ASP.NET Web Forms project that uses jQuery to make RESTful calls to a WCF service.

    Figure 2

    Solution explorer view

  4. Right-click on CustomerService.svc in the CustomersService project and select View in Browser from the menu. This will start a local WCF server and show a test page.
  5. Back in Visual Studio, right-click on the CustomerViewer project and select Set as StartUp Project from the menu.
  6. Run the application by pressing F5. The first time the application runs there will be short delay before data is loaded.
  7. Once data loads, notice that customers appear in the ComboBox. Once a customer is selected the details are shown in the form allowing customer data to be updated or deleted.
  8. Back in Visual Studio, right-click on CustomerForm.cs or CustomerForm.vb (depending upon your language) and select View Code from the menu. Take a moment to explore the code and note the following:
    1. A WCF service proxy is used to call a service that supplies customer data
    2. Control data bindings are defined in the SetBindings method
    3. Customer data can be updated and deleted through interactions with the WCF service
  9. Locate the CustomerService.Model project and double-click the AdventureWorksLT.edmx file to see the Entity Framework 4 model that's exposed. The entity model contains several entities including Customer which is used by the Windows Forms application.
    Note:
    DataSets are not used in the application since data will exposed through a Web Service and consumed by many different types of clients. The CustomerService.Model project contains strongly-typed objects that work well in a service-oriented environment. Strongly-typed objects also fit in well with Silverlight applications where the DataSet class and related classes such as DataTable aren't supported. By using a Web Service to expose strongly-typed data, many different types of applications can interact with the data regardless of technology or framework.
  10. Open CustomerRepository in the Repository folder and take a moment to look through the code that interacts with the entity model (you might also want to look at the base class named RepositoryBase). This class is responsible for all communication with Entity Framework and acts as a re-useable repository layer in the application.
  11. Locate the CustomerService project and view ICustomerService in the editor to see the operations it exposes. The operations are used to load Customer objects and handle update and delete operations. The Windows Forms project currently uses a WCF service proxy object to communicate with the different service operations and the Silverlight project will need to call the same service. Service calls are forwarded to the CustomerRepository class examined earlier.

    Note:
    WCF services work well in environments where data must be exposed to different types of clients without requiring a specific technology or framework. The application shown in this lab uses WCF services to promote data re-use, allow different types of clients to consume data, and provide a standards-compliant way to access data.