ASP.NET
  Home arrow ASP.NET arrow Page 2 - Developing Business Logic using the WCF Se...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Developing Business Logic using the WCF Service Library with VS2K8 and ASP.NET 3.5
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2007-10-03

    Table of Contents:
  • Developing Business Logic using the WCF Service Library with VS2K8 and ASP.NET 3.5
  • Understanding the WCF Service Library: Employee class
  • Understanding the WCF Service Library: the app.config file
  • Understanding the WCF Service host
  • Understanding the WCF Client: ASP.NET 3.5 web site

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    More ASP.NET Articles
    More By Jagadish Chaterjee


       · Hello guys,This is my third article in the series focusing on WCF development...
       · Do you differentiate at all between the service layer and the business layer. isnt...
       · Simply don't want to complicate the things at this moment. You will see several...
     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT