Chapter 12. Applying LINQ to SQL to a WCF Service

Now that we have learned all of the new features for C# 3.0, including LINQ and LINQ to SQL, we will use them in the data access layer of a WCF service. We will create a new WCF service, which is very similar to the one we created in the previous chapters, but in this service, we will use LINQ to SQL to connect to the Northwind database, to retrieve and update a product.

In the data access layer, we will use LINQ to SQL to retrieve product information from the database, and return it to the business logic layer. You will see that with LINQ to SQL, we will need only one LINQ statement in the GetProduct method, and we will no longer need to worry about the database connection, or the actual query statement.

In this chapter, we will also learn how to update a product with LINQ to SQL in the data access layer. We will see how to attach an entity object to LINQ to SQL DataContext, and leave all of the update work to LINQ to SQL, and will also see how to control the concurrency of updates with LINQ to SQL.

In this chapter, we will cover:

  • Creating the solution using Service Factory
  • Modeling the WCF service using Service Factory
  • Generating source code for the service
  • Modeling the Northwind database in LINQ to SQL designer
  • Implementing the data access layer using LINQ to SQL
  • Implementing the business logic layer
  • Implementing the service interface layer
  • Modeling the host application and the test client
  • Implementing the test client
  • Testing the get and update operations of the WCF service
  • Testing concurrent updates with LINQ to SQL

Creating the LINQNorthwind solution

From this point on on, in the first few sections of this chapter, we will use Service Factory for creating the solution files, modeling the service, and generating the source code. The steps here are very similar to those discussed in Chapter 7, so we will not have screenshots for every step. You can follow the steps here to quickly create the solution, and refer back to Chapter 7 for detailed instructions if you have any doubts. You can also download the source code for this chapter, if you don't want to repeat all of these steps.

One thing that is different here is that we will add an operation of UpdateProduct in this chapter, so that we can test the concurrent updates with LINQ to SQL later on.

To start, follow these steps to create the initial solution files:

  1. Start Visual Studio 2008.
  2. Select menu option File | New | Project..
  3. Select Guidance Packages | Service Factory: Modeling Edition as the Project type, and Model Project as the Template.
  4. Enter LINQNorthwind as the Name, and leave the Location as the default value (D:SOAwithWCFandLINQProjects).
  5. Click OK.

    Note

    The Guidance Packages project type will be shown only after you have the Guidance Packages installed. You should have installed Guidance Packages in Chapter 7.

  6. Change the model project name from LINQNorthwind to LINQNorthwind Models.
..................Content has been hidden....................

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