Developing Business Logic using the WCF Service Library with VS2K8 and ASP.NET 3.5 - Understanding the WCF Service Library: Employee class
(Page 2 of 5 )
The "Employee" class mimics the "emp" table. For the simplicity of this article, I added a table called "emp" as follows:

The "Employee" class is specially created to store one row of the above table in the form of an object. It simply contains the properties of each of the columns available in the above table. It is defined as follows:
<DataContract()>
Public Class Employee
Private _Empno As Integer
Private _Ename As String
Private _Sal As Double
Private _Deptno As Integer
<DataMember()> _
Public Property Empno() As Integer
Get
Return _Empno
End Get
Set(ByVal value As Integer)
_Empno = value
End Set
End Property
<DataMember()> _
Public Property Ename() As String
Get
Return _Ename
End Get
Set(ByVal value As String)
_Ename = value
End Set
End Property
<DataMember()> _
Public Property Sal() As Double
Get
Return _Sal
End Get
Set(ByVal value As Double)
_Sal = value
End Set
End Property
<DataMember()> _
Public Property Deptno() As Integer
Get
Return _Deptno
End Get
Set(ByVal value As Integer)
_Deptno = value
End Set
End Property
End Class
Understanding the WCF Service Library: IEmpService interface and EmpService class
To enable applications to utilize the services of WCF Service, an interface must be defined. The client accesses business objects and executes the methods which are defined in this interface.
"IEmpService" is an interface defined exclusively for the above purpose. It contains all the methods which are accessible to the client. In this case, all the CRUD methods concern the "emp" table. It is defined as follows:
Imports System.Data
<ServiceContract()> _
Public Interface IEmpService
<OperationContract()> _
Function GetInfo(ByVal EmpNo As Integer) As Employee
<OperationContract()> _
Function GetLookup() As DataSet
<OperationContract()> _
Function GetList() As DataSet
<OperationContract()> _
Sub Add(ByVal p_emp As Employee)
<OperationContract()> _
Sub Update(ByVal p_emp As Employee)
<OperationContract()> _
Sub Delete(ByVal p_emp As Employee)
End Interface
The above is simply an interface, which always contain abstract methods (or methods without any code). It has to be implemented using another class to make it as a concrete class. "EmpService" is the class which implements the structure defined in "IEmpService" interface and it is defined as follows:
Imports System.Data.SqlClient
Public Class EmpService
Implements IEmpService
Public Function GetInfo(ByVal EmpNo As Integer) As Employee
Implements IEmpService.GetInfo
Using cn As New SqlConnection(My.Settings.cnNorthwind)
Using cmd As New SqlCommand("SELECT empno,ename,sal,deptno FROM
dbo.emp WHERE empno=" & EmpNo, cn) cmd.Connection.Open()
Using rdr As SqlDataReader = cmd.ExecuteReader
If rdr.Read() Then
Return New Employee With {.Empno = rdr(rdr.GetOrdinal("Empno")),
.Ename = rdr(rdr.GetOrdinal("Ename")) & "", .Sal = rdr
(rdr.GetOrdinal("sal")) & "", .Deptno = rdr(rdr.GetOrdinal
("Deptno")) & ""} Else
Return Nothing
End If
End Using
cmd.Connection.Close()
End Using
End Using
End Function
Public Function GetList() As System.Data.DataSet Implements
IEmpService.GetList
Dim ds As New DataSet
Using cn As New SqlConnection(My.Settings.cnNorthwind)
Using cmd As New SqlCommand("SELECT empno,ename,sal,deptno FROM
dbo.emp ", cn) Using da As New SqlDataAdapter(cmd)
da.Fill(ds)
End Using
End Using
End Using
Return ds
End Function
Public Function GetLookup() As System.Data.DataSet Implements
IEmpService.GetLookup
Dim ds As New DataSet
Using cn As New SqlConnection(My.Settings.cnNorthwind)
Using cmd As New SqlCommand("SELECT empno, convert(varchar,empno)
+ ' - ' + ename as ename FROM dbo.emp ", cn) Using da As New SqlDataAdapter(cmd)
da.Fill(ds)
End Using
End Using
End Using
Return ds
End Function
Public Sub Add(ByVal p_emp As Employee) Implements
IEmpService.Add
Using cn As New SqlConnection(My.Settings.cnNorthwind)
Using cmd As New SqlCommand("INSERT INTO emp
(empno,ename,sal,deptno) VALUES (" & p_emp.Empno & ", '" &
p_emp.Ename & "'," & p_emp.Sal & "," & p_emp.Deptno & ")", cn) cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
End Using
End Using
End Sub
Public Sub Update(ByVal p_emp As Employee) Implements
IEmpService.Update
Using cn As New SqlConnection(My.Settings.cnNorthwind)
Using cmd As New SqlCommand("UPDATE emp SET ename='" &
p_emp.Ename & "', sal=" & p_emp.Sal & ", deptno=" & p_emp.Deptno
& " WHERE empno = " & p_emp.Empno, cn) cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
End Using
End Using
End Sub
Public Sub Delete(ByVal p_emp As Employee) Implements
IEmpService.Delete
Using cn As New SqlConnection(My.Settings.cnNorthwind)
Using cmd As New SqlCommand("DELETE FROM emp WHERE empno = " &
p_emp.Empno, cn) cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
End Using
End Using
End Sub
End Class
Next: Understanding the WCF Service Library: the app.config file >>
More ASP.NET Articles
More By Jagadish Chaterjee