Using LINQ to SQL for creating the service

LINQ to SQL is the family of LINQ technologies targeting relational databases where data is mapped as objects. LINQ to SQL provides the fastest way to create an interface to the backend. The only disadvantage is it can only map one level of relationship. So, if you need deeper relationship management, the Entity Framework is the way to go. Some of the features of LINQ to SQL are as follows:

  • Translates LINQ expression to T-SQL (Transact-SQL)
  • Provides an easy way to query objects instead of SQL statements
  • Has the ability to create databases and tables in the database
  • Provides a way to query the database using regular T-SQL statements

In this recipe, let's build the MyTask service like the preceding recipe, but instead of using the Entity Framework we will use LINQ to SQL classes.

Getting ready

Open the Visual Studio and create a new project using the WCF Service Application template and name it Recipe4_LINQToSQL under the CH7 folder. Right-click on App_Data and select Add new item. Navigate to the preceding recipe's App_Data folder and select the MyTask.mdf file. We will use the same database and the MyTask table.

How to do it...

For this recipe, we will again be creating the service application, but this time we will use LINQ to SQL to create the entity objects.

  1. Delete IService1.cs and Service1.svc from the project.
  2. Right-click on the project and Add new item. Select the LINQ to SQL Classes template and name it MyTaskDataModel, as shown in the following screenshot. When you click on Add, you should see the MyTaskDataModel.dbml file in your project.
    How to do it...
  3. Now let's add another new item to the project. Select WCF Service as the file template. Name this file MyTaskService.svc.
  4. Open the MyTaskService.cs file and add a get function to return the MyTask class:
    [ServiceContract]
    public interface IMyTaskService
    {
    [OperationContract]
    MyTask GetTask(int value);
    }
    
  5. Now, open the MyTaskService.svc.cs file and let's add the details for the GetTask function. In this method, we shall create a data context object and use the LINQ syntax for getting the task by Id:
    public class MyTaskService : IMyTaskService
    {
    public MyTask GetTask(int value)
    {
    MyTaskDataModelDataContext ctx = new MyTaskDataModelDataContext();
    return ctx.MyTasks.Where(c => c.Id == value).Single();
    }
    }
    
  6. Right-click on MyTaskService.svc and set it as start page. Now press F5 to run. You should see the service in the WCF Test Client. Select the GetTask request and type a value of 2 and click on Invoke. You should see the following screenshot:
    How to do it...
  7. Now let's create a client application to consume this data service exactly like the preceding recipe. Create a new Phone 7 Application and name it Recipe4_LINQToSQLClient.
  8. Add Service Reference; you will be prompted by a dialog box like shown in the following screenshot:
    How to do it...
  9. Add the context object and the event handler similar to steps 18, 19, and 20 of the previous recipe.
  10. Press F5. You should get the main page as shown in the following screenshot. When you type the Task Id and click on Get Task Details you should get the details of the task.
    How to do it...

How it works...

LINQ to SQL creates the .dbml file. This file is the proxy file generated by LINQ with the data context object. This file has the class definition for the table, relationships, and properties.

If you open the file MyTaskDataModel.designer.cs, then you can see the following data context class. This class connects to the database using a connection string that is saved in the config file.

public partial class MyTaskDataModelDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
#endregion
public MyTaskDataModelDataContext() : base(global::System.Configuration.ConfigurationManager. ConnectionStrings["MyTasksConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}

We can also see how all the records in the MyTask table are returned in the following code:

public System.Data.Linq.Table<MyTask> MyTasks
{
get
{
return this.GetTable<MyTask>();
}
}

LINQ to SQL also creates the MyTask class with all the table columns as properties. LINQ to SQL supports many types of attributes such as TableAttribute, ColumnAttribute, AssociationAttribute, FunctionAttribute, table, and so on. We can see the different values of ColumnAttribute in this file.

There's more...

In this sample, we just created a simple recipe that invokes a GetTask method to get a specific task row. Using the DataContext class, we can also update and delete records in the table.

See also

In an earlier recipe, WCF using ADO.NET Entity Framework, we covered how to use the Entity Framework. The EF is considered more robust than LINQ to SQL. So, for databases with deeper relationships and for more scalability use the Entity Framework.

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

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