0% found this document useful (0 votes)
46 views

Aase Implementasi Web Service Implementasi Web Service: OOP, Database, Tipe Data, Pic Blob

The document discusses object-oriented programming (OOP) concepts in .NET such as classes, inheritance, interfaces, abstract classes, properties, and methods. It provides examples of implementing interfaces, inheritance between classes, and abstract classes. It also covers OOP concepts when creating web services in .NET, including using primitive data types, enums, classes, and arrays as data types. It describes how to create a web service project in Visual Studio, add web references in a client project to call the web service methods, and provides examples of number and picture web services with databases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Aase Implementasi Web Service Implementasi Web Service: OOP, Database, Tipe Data, Pic Blob

The document discusses object-oriented programming (OOP) concepts in .NET such as classes, inheritance, interfaces, abstract classes, properties, and methods. It provides examples of implementing interfaces, inheritance between classes, and abstract classes. It also covers OOP concepts when creating web services in .NET, including using primitive data types, enums, classes, and arrays as data types. It describes how to create a web service project in Visual Studio, add web references in a client project to call the web service methods, and provides examples of number and picture web services with databases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

AASE

Implementasi Web Service


OOP, Database, Tipe Data,
Pic BLOB

OOP di .NET
Class, Generalisasi, Interface

OOPWS\ConsoleApplication1

Implements Interface
Konstruktor
Property Gaji (get & set)
Agar bisa dioverride oleh subclass
Overloading juga!

Implementasi dari Interface

Property Nama (get & set)

Contoh Interface dan Inheritance


Public Class Manager
Sub class
Inherits Employee
Public Overrides Function showGaji() As Decimal
Return MyBase.Gaji + 30
Override
End Function
End Class
Interface
Public Interface Orang
Property Nama() As String
Function Bicara() As String
End Interface

Hak Akes Property

Instilah-istilah OOP di VB.NET


Current Class : MyClass
Parent Class : MyBase
Abstract Class : MustInherit
Method Abstract : MustOverride
Extends : Inherits
Agar method bisa dioverride : Overridable
Ketika mengoverride method : Overrides
Static : Shared
Interface = Implements
Overloading: overloads (optional)

Employee memiliki Account


Private _acc As Account
Public Property AccProperty() As Account
Get
Return _acc
End Get
Set(ByVal value As Account)
_acc = value
End Set
End Property

Public MustInherit Class Account


Abstract Class
Private _intAccountNumber As Integer
Public MustOverride Sub Deposit(ByVal Amount As Double)
Public MustOverride Sub Withdraw(ByVal Amount As Double)
Public MustOverride Function getBalance() As Double
End Class

Class CheckingAccount
Property ReadOnly
Method biasa
Implementasi Abstract

Implementasi Abstract

Implementasi Abstract

Main Class

Web Service dengan .NET


Procedure / Subroutine : adalah suatu kumpulan perintahperintah yang digunakan untuk suatu tujuan tertentu dan diberi
nama tertentu.
Procedure tidak mengembalikan nilai
Di dalam VB : keywordnya sub end sub
Tidak ada keyword return
Function : adalah suatu kumpulan perintah-perintah yang
digunakan untuk suatu tujuan tertentu dan diberi nama tertentu
serta mengembalikan nilai tertentu keluar kepada fungsi yang
memanggilnya.
Function mengembalikan nilai
Di dalam VB : keywordnya function . end function
Ada keyword return

Contoh Procedure
Dalam VB:
Private Sub LuasPersegiPanjang(ByVal panjang as Integer,ByVal lebar
as Integer)
Dim luas as Integer
luas = panjang * lebar
Console.WriteLn(Luas = & Str(luas))
End Sub
Dalam C#:
private void LuasPersegiPanjang(int panj, int lebar){
int luas;
luas = panj * lebar;
Console.WriteLn(Luas = + Convert.ToString(luas));
}

Contoh Function
Dalam VB:
Private Function LuasPersegiPanjang(ByVal panjang as
Integer,ByVal lebar as Integer) as Integer
Return panjang*lebar;
End Function
Console.WriteLn(Luas = & LuasPersegiPanjang(5,3));
Dalam C#:
private int LuasPersegiPanjang(int panj, int lebar){
return panj*lebar;
}
Console.WriteLn(Luas = + LuasPersegiPanjang(5,3));

Contoh OOPWebServices

Project: WSOOP

Lanjutan

Database WebService

Database WebService
Buatlah variable private beripe string, misal
bernama strConnection yang berisi cara
koneksi database
Buatlah variabel private bertipe string, misal
bernama strSQL yang berisi SQL query yang
bergantung pada query yang ingin kita lakukan
Buatlah variabel private bertipe class
SqlConnection, misal bernama oSqlConnection
untuk obyek koneksi database yang akan
dibangun berdasarkan strConnection

Contoh Connection
Private oSqlConnection As SqlConnection =
New SqlConnection(strConn) //untuk
SQLServer
Private oOleDbConnection As
OleDbConnection = New
OleDbConnection(strConn) //untuk OleDb
Private oMySqLConnection As
MySqLConnection = New
MySqLConnection(strConn) //untuk MySQL

Database WebService
Buatlah variabel private beripe class
SqlCommand, misalnya bernama
oSqlCommand untuk obyek perintah SQL
Query yang akan dilakukan melalui
SQLConnection yang telah kita buat
Berikan perintah pada SQLCommand bertipe
CommandType.Text jika perintah SQL biasa
Berikan perintah pada SQLCommand bertipe
CommandType.StoredProcedure jika
perintah berupa stored procedure

Contoh Command
Private oSqlCommand As SqlCommand //untuk SQL
Server
Private oOleDbCommand As OleDbCommand //untuk
OleDB
Me.oSqlCommand = New SqlCommand(Me.strSQL,
Me.oSqlConnection) //untuk SQL Server
Me.oOleDbCommand = New OleDbCommand(Me.strSQL,
Me.oOleDbConnection) //untuk OleDb
Me.oSqlCommand.CommandType = CommandType.Text
Me.oSqlCommand.CommandText = strSQL

DataReader
Buatlah obyek dari SqlDataReader jika kita
ingin membaca database menggunakan
perintah select
Kemudian panggil method ExecuteReader
dari obyek SqlCommand yang akan
membaca database per-record dengan
menggunakan looping (method read) yang
terlebih dahulu memanggil method open()
dari obyek SqlConnection

Contoh DataReader
Dim oSqlDataReader as SqlDataReader
Me.oSqlConnection.open()
Me.oSqlDataReader =
Me.oSqlCommand.ExecuteReader()
Dim Nama As String
If oSqlDataReader.HasRows Then
While oSqlDataReader.Read() Do
Nama = oSqlDataReader(Nama)
End While
End If

Contoh DataReader (2)


Me.oSqlConnection.open()
Int hasil =
Me.oSqlCommand.ExecuteNonQuery
If hasil = 1 Then
Response.Write(Sukses!)
Else
Response.Write(Gagal!)
End If

Contoh DataSet
Me.strSQL = "select * from user"
Me.objDataAdapter = New
OleDbDataAdapter(Me.strSQL, Me.strConn)
Dim ds As DataSet = New DataSet
Me.objDataAdapter.Fill(ds)

Tipe Data dan Parameter WS.NET


Primitive Type
String, Char, Byte, Boolean, Int16, Int32, Single, Double,
DateTime

Enum Type
Public Enum Warna
Merah
Kuning
Hijau

End Enum
Dim warnaku as Warna = Warna.Merah

Class
DataSet
Array

Contoh-contoh
Enum:
Public Enum Warna
Merah
Kuning
Hijau
End Enum

Idx diisi 2

<WebMethod()> _
Public Function CobaEnum(ByVal idx As Integer) As Warna
Select Case idx
Case 1
Return Warna.Merah
Case 2
Return Warna.Kuning
Case Else
Return Warna.Hijau
End Select
End Function

Tipe Data Primitif : String dan Integer


<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
<WebMethod()> _
Public Function Jumlahkan(ByVal a As
Integer, ByVal b As Integer) As Integer
Return a + b
End Function

Array

<WebMethod()> _
Public Function JmlMatrik() As Integer()()
Dim a As Integer() = {1, 2, 3}
Dim b As Integer() = {4, 5}
Dim hasil()() As Integer = {a, b}
Return hasil
End Function
Array multi dimensi tidak support, harus jagged array

Class
Public Class Mahasiswa
Private _nim As String
Private _nama As String
Private _ipk As Double
Property Nim() As String
Get
Return _nim
End Get
Set(ByVal value As String)
_nim = value
End Set
End Property
Property Nama() As String
Get
Return _nama
End Get
Set(ByVal value As String)
_nama = value
End Set
End Property
Property IPK() As Double
Get
Return _ipk
End Get
Set(ByVal value As Double)
_ipk = value
End Set
End Property
Public Function Bicara() As String
Return "Saya bernama " & Nama() & ", NIM saya " & Nim() & " dan IPK adalah " & IPK()
End Function
End Class

Class
<WebMethod()> _
Public Function getMahasiswa(ByVal mynim As String) As Mahasiswa
Dim m As Mahasiswa = New Mahasiswa()
If mynim = "22002529" Then
m.Nim = "22002529"
m.Nama = "anton"
m.IPK = 3.68
Else
m.Nim = "22002521"
m.Nama = "mahas"
m.IPK = 3.54
End If
Return m
End Function

Buat Web Service


Gunakan VS 2008
New Web Site -> ASP.NET Web Service
Project
Tuliskan method-method public-nya pada
code Web Service
Compile + Execute
Perhatikan port yg dipakai, jgn tutup hasil run
tersebut, sementara buat clientnya!

Buat Client
Bisa diakses dari desktop, web, dan mobile
Buat aplikasi client seperti biasa
Dari solution explorer, klik kanan, add Web
References
Masukkan URL Web Service
Add reference
Perhatikan nama referensinya

Import nama referensinya, instansiasi, dan gunakan


sebagai variable dalam aplikasi kita
Gunakan method nya!

Contoh
Cara instansiasi:
Private ws As
ServiceReference1.NumberGuestClient = New
ServiceReference1.NumberGuestClient

Cara penggunaan:
Label1.Text = ws.IsMatch(Val(TextBox1.Text))

Demo
NumberGuest -> NumberGuest Project
Database WS -> BukuService Project
Blob Picture WS -> WS-Blob Project

Demo Picture WS
Tipe data BLOB
Kembalikan array of byte
Ditampilkan pada PictureBox

Next
REST Web Service in .NET

You might also like