Friday, July 11, 2008

Web Service in .NET

Web service adalah komponen layakan aplikasi yang didesain untuk mendukung interaksi antar aplikasi dan integrasi aplikasi yang biasanya diserialisasi dengan menggunakan XML (eXtensible Markup Language) (dalam teknologi WSE (Web Service Enchanment) memungkinkan web service untuk diserialisasi sebagai binary) dan dapat diakses melalui protokol terbuka yaitu SOAP (Simple Object Access Protocol) dengan bahasa WSDL (Web Service Description Language) dan terdaftar dalam UUDI (Universal Discovery Description and Integration). XML sendiri adalah sebuah standart yang digunakan untuk mendefinisikan data dalam format yang sederhana dan fleksibel.
Web Service menjadi populer saat ini karena web service mampu mengintegrasikan aplikasi yang berbeda platform secara lebih sederhana dan mampu memperbaiki kelemahan dari teknologi sistem terdistribusi lainnya seperti RPC (Remote Procedure Call), Java RMI (Java Remote Method Invocation) maupun arsitektur CORBA (Common Object Request Broker Architecture). Kelemahan utama dari sistem terdistribusi tersebut adalah tidak mendukung heterogenoitas karena masing-masing teknologi mempunyai standar protokol sendiri-sendiri.
Web services sendiri merupakan komponen yang independen terhadap platform ataupun bahasa. Web services menggunakan protokol HTTP (HyperText Transfer Protocol) yang merupakan protokol yang sangat umum digunakan. Dimana protokol tersebut sangat mendukung heterogenoitas dan interoperabilitas serta memudahkan integrasi. Untuk lebih jelasnya bisa dilihat ilustrasi dari cara kerja web service seperti yang diilustrasikan pada gambar dibawah ini :



using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Collections.Generic;
using TesoLibrary;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
private GuideLineLogic guideLineLogic;
private ProjectMemberLogic projectMemberLogic;
private EmployeeLogic employeeLogic;
private EvaluationGroupLogic evaluationGroupLogic;
private EvaluationItemLogic evaluationItemLogic;
private ProjectLogic projectLogic;

public Service () {
Util.Instance.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
this.guideLineLogic = new GuideLineLogic();
this.projectMemberLogic = new ProjectMemberLogic();
this.employeeLogic = new EmployeeLogic();
this.evaluationGroupLogic = new EvaluationGroupLogic();
this.evaluationItemLogic = new EvaluationItemLogic();
this.projectLogic = new ProjectLogic();
}

[WebMethod]
public void InsertGuideLine(GuideLine item)
{
this.guideLineLogic.Insert(item);
}

[WebMethod]
public void UpdateGuideLine(GuideLine item)
{
this.guideLineLogic.Update(item);
}

[WebMethod]
public void DeleteGuideLine(GuideLine item)
{
this.guideLineLogic.Delete(item);
}

[WebMethod]
public GuideLine FetchGuideLine(string criteria)
{
return this.guideLineLogic.GetObject(criteria);
}

[WebMethod]
public List RetrieveGuideLine()
{
return this.guideLineLogic.GetObjects();
}

[WebMethod]
public void InsertProjectMember(ProjectMember item)
{
this.projectMemberLogic.Insert(item);
}

[WebMethod]
public void UpdateProjectMember(ProjectMember item)
{
this.projectMemberLogic.Update(item);
}

[WebMethod]
public void DeleteProjectMember(ProjectMember item)
{
this.projectMemberLogic.Delete(item);
}

[WebMethod]
public ProjectMember FetchProjectMember(string criteria)
{
return this.projectMemberLogic.GetObject(criteria);
}

[WebMethod]
public List RetrieveProjectMember()
{
return this.projectMemberLogic.GetObjects();
}

[WebMethod]
public void InsertEmployee(Employee item)
{
this.employeeLogic.Insert(item);
}

[WebMethod]
public void UpdateEmployee(Employee item)
{
this.employeeLogic.Update(item);
}

[WebMethod]
public void DeleteEmployee(Employee item)
{
this.employeeLogic.Delete(item);
}

[WebMethod]
public Employee FetchEmployee(string criteria)
{
return this.employeeLogic.GetObject(criteria);
}

[WebMethod]
public List RetrieveEmployee()
{
return this.employeeLogic.GetObjects();
}

[WebMethod]
public void InsertEvaluationGroup(EvaluationGroup item)
{
this.evaluationGroupLogic.Insert(item);
}

[WebMethod]
public void UpdateEvaluationGroup(EvaluationGroup item)
{
this.evaluationGroupLogic.Update(item);
}

[WebMethod]
public void DeleteEvaluationGroup(EvaluationGroup item)
{
this.evaluationGroupLogic.Delete(item);
}

[WebMethod]
public EvaluationGroup FetchEvaluationGroup(string criteria)
{
return this.evaluationGroupLogic.GetObject(criteria);
}

[WebMethod]
public List RetrieveEvaluationGroup()
{
return this.evaluationGroupLogic.GetObjects();
}

[WebMethod]
public void InsertEvaluationItem(EvaluationItem item)
{
this.evaluationItemLogic.Insert(item);
}

[WebMethod]
public void UpdateEvaluationItem(EvaluationItem item)
{
this.evaluationItemLogic.Update(item);
}

[WebMethod]
public void DeleteEvaluationItem(EvaluationItem item)
{
this.evaluationItemLogic.Delete(item);
}

[WebMethod]
public EvaluationItem FetchEvaluationItem(string criteria)
{
return this.evaluationItemLogic.GetObject(criteria);
}

[WebMethod]
public List RetrieveEvaluationItem()
{
return this.evaluationItemLogic.GetObjects();
}

[WebMethod]
public void InsertProject(Project item)
{
this.projectLogic.Insert(item);
}

[WebMethod]
public void UpdateProject(Project item)
{
this.projectLogic.Update(item);
}

[WebMethod]
public void DeleteProject(Project item)
{
this.projectLogic.Delete(item);
}

[WebMethod]
public Project FetchProject(string criteria)
{
return this.projectLogic.GetObject(criteria);
}

[WebMethod]
public List RetrieveProject()
{
return this.projectLogic.GetObjects();
}

[WebMethod]
public bool LoginUser(string name, string password)
{
List list = new List();
foreach (Employee item in list)
{
if (item.UserName.Trim().Equals(name))
{
if (item.Password.Trim().Equals(password))
{
return true;
}
}
}
return false;
}
}

Using MVC Concept in .NET

  1. Entity

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TesoLibrary
    {
    public class Employee
    {
    private string _empId;
    private string _name;
    private string _role;
    private string _roleApp;
    private string _userName;
    private string _password;

    public string EmpId
    {
    get { return _empId; }
    set { _empId = value; }
    }

    public string Name
    {
    get { return _name; }
    set { _name = value; }
    }

    public string Role
    {
    get { return _role; }
    set { _role = value; }
    }

    public string RoleApp
    {
    get { return _roleApp; }
    set { _roleApp = value; }
    }

    public string UserName
    {
    get { return _userName; }
    set { _userName = value; }
    }

    public string Password
    {
    get { return _password; }
    set { _password = value; }
    }

    public Employee(string empId, string name, string role, string roleApp, string userName, string password)
    {
    this._empId = empId;
    this._name = name;
    this._role = role;
    this._roleApp = roleApp;
    this._userName = userName;
    this._password = password;
    }

    public Employee()
    {
    this._empId = string.Empty;
    this._name = string.Empty; ;
    this._role = string.Empty; ;
    this._roleApp = string.Empty; ;
    this._userName = string.Empty; ;
    this._password = string.Empty; ;
    }
    }
    }

  2. Data Access

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;

    namespace TesoLibrary
    {
    public class EmployeeDataAccess: DataAccessAbstract
    {
    protected override SqlCommand GetInsertCommand(Employee item)
    {
    SqlCommand command = new SqlCommand();
    command.CommandText = "insert into employee(empid, name, role, roleapp, username, password) values(" + "'" + item.EmpId + "','" + item.Name + "','" + item.Role + "','" + item.RoleApp + "','" + item.UserName + "','" + item.Password + "')";
    return command;
    }

    protected override SqlCommand GetUpdateCommand(Employee item)
    {
    SqlCommand command = new SqlCommand();
    command.CommandText = "update employee set name = '" + item.Name + "'" + ", role = '" + item.Role + "'" + ", roleapp = '" + item.RoleApp + "'" + ", username = '" + item.UserName + "'" + ", password = '" + item.Password + "'" + " where empid = '" + item.EmpId + "'";
    return command;
    }

    protected override SqlCommand GetDeleteCommand(Employee item)
    {
    SqlCommand command = new SqlCommand();
    command.CommandText = "delete from employee where empid = '" + item.EmpId + "'" ;
    return command;
    }

    protected override SqlCommand GetObjectCommand(K criteria)
    {
    SqlCommand command = new SqlCommand();
    command.CommandText = "select empid, name, role, roleapp, username, password from employee where empid = " + criteria;
    return command;
    }

    protected override SqlCommand GetObjectsCommand()
    {
    SqlCommand command = new SqlCommand();
    command.CommandText = "select empid, name, role, roleapp, username, password from employee";
    return command;
    }

    protected override Employee MapObject(SqlDataReader reader)
    {
    Employee item = new Employee(reader["empid"].ToString(), reader["name"].ToString(), reader["role"].ToString(), reader["roleapp"].ToString(), reader["username"].ToString(), reader["password"].ToString());
    return item;
    }
    }
    }

  3. Logic

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TesoLibrary
    {
    public class EmployeeLogic
    {
    public EmployeeDataAccess _dataAccess = new EmployeeDataAccess();

    public void Insert(Employee item)
    {
    this._dataAccess.Insert(item);
    }

    public void Update(Employee item)
    {
    this._dataAccess.Update(item);
    }

    public void Delete(Employee item)
    {
    this._dataAccess.Delete(item);
    }

    public Employee GetObject(string criteria)
    {
    return this._dataAccess.GetObject(criteria);
    }

    public List GetObjects()
    {
    return this._dataAccess.GetObjects();
    }

    public Employee AuthenticateUser(string userName, string password)
    {
    string criteria = " where Username='" + userName + "' and Password='" + password + "'";
    return this._dataAccess.GetSpecificObject(criteria);
    }

    }
    }