Designing WCF DataContract Classes Using the LINQ to SQL Designer - Creating WCF DataContract classes visually using LINQ to SQL Designer
(Page 2 of 5 )
If you are new to WCF, please refer my article available at http://www.aspfree.com/c/a/BrainDump/Developing-an-Object-Oriented-Business-Component-Using-WCF-and-Visual-Studio-2008/
The entity classes (which hold information) are called DataContract classes in WCF. If the WCF Service has many DataContract classes, we will generally code them manually. The class designer can help us to a certain extent, but not a lot.
I have authored quite a few articles on "LINQ to SQL" on this web site. It is another great technology from Microsoft which enables us to create a DAL (Data Access Layer) in a RAD manner. Now we have the LINQ to SQL Designer. It is very similar to a class designer. However, it is tightly integrated with database objects.
In all of my previous articles, I used "LINQ to SQL" or "LINQ to SQL Designer" to simply leverage the capabilities of a DAL. However, we can also use the designer to quickly create WCF-supported "DataContract" classes visually and much more efficiently than a class designer.
Let us start creating a new solution to demonstrate this:
Create a new Visual Studio 2008 solution.
Add a new WCF Service project named "DemoEmpService."
Modify all "Service1" to "EmpService" everywhere in the project (steps are shown in my previous articles).
Add a "LINQ to SQL Classes" item to the project ("DemoEmp.dbml").
Using "Server Explorer," drag and drop "Emp" and "Dept" tables as shown in the following figure.

The above automatically creates the respective database context, along with "DataContract" classes with respect to "Emp" and "Dept." Make sure that you don't forget to set "SerializationMode" to "UniDirectional" (otherwise it won't create "DataContract" classes).
Okay. Sometimes, we may need to create some custom strongly-typed DataContract classes. These DataContract classes may not have any object directly related to the database (they may be part of a business information exchange). Even in these scenarios, we can make use of the LINQ to SQL designer.

Rename the class to "EnameDname" and add two properties (by right clicking on the class in the designer), "Ename" and "Dname."
Set the "SerializationMode" to "UniDirectional" for the context.
Save the DBML.
At this point, if you open the "EmpViews.designer.vb," you should see the "EnameDname" class defined as the "DataContract" class as follows:
<Table(Name:=""), _
DataContract()> _
Partial Public Class EnameDname
Private _Ename As String
Private _Dname As String
Public Sub New()
MyBase.New
End Sub
<Column(Storage:="_Ename", CanBeNull:=false), _
DataMember(Order:=1)> _
Public Property Ename() As String
Get
Return Me._Ename
End Get
Set
If (String.Equals(Me._Ename, value) = false) Then
Me._Ename = value
End If
End Set
End Property
<Column(Storage:="_Dname", CanBeNull:=false), _
DataMember(Order:=2)> _
Public Property Dname() As String
Get
Return Me._Dname
End Get
Set
If (String.Equals(Me._Dname, value) = false) Then
Me._Dname = value
End If
End Set
End Property
End Class
The above class can be directly used as a "WCF data contract object exchange" without any modification.
Next: Creating WCF DataContract classes visually using LINQ to SQL Designer: Accessing in Silverlight >>
More Windows Scripting Articles
More By Jagadish Chaterjee