I really wanted to ask this question, but took the time to do it now.
In any case, we discussed web services (yes, these traditional SOAP-XML response services) and RESTful services (which now have many developers).
I feel that although I understand the concept of REST in general, I need to know more. I think one of the best ways to fully cover this is to show that it is really better (emphasis on **, because the subjective word is better), like what was being done at the moment.
Consider the following simple traditional codes: (This instance is copied from a corporate application with Oracle as a backend. The database, to a large extent, I think, does not matter, since you could easily switch between SQL Server or Oracle or any database on this) .
myWebService.asmx.cs
namespace MyApplication { public class myWebService : System.Web.Services.WebService { private classEmployee _emp = new classEmployee(); [WebMethod] public string GetEmployees() { string EmployeeData = string.Empty; EmployeeData = _emp.GetEmployees(); return EmployeeData; } } }
classEmployee.cs
using System; using System.Collections.Generic; using System.Data; using System.Globalization; using System.Data.OracleClient; namespace MyApplication.App_Code { public class classEmployee { private DataAccess _da; public string GetEmployees() { string employeeData = string.Empty; string cmd = string.Empty; OracleCommand oraCmd = new OracleCommand(); DataSet ds = new DataSet(); try { cmd = "SELECT * FROM Employees"; oraCmd.CommandType = CommandType.Text; oraCmd.CommandText = cmd; ds = (DataSet)_da.ExecSQLQueryCmd(oraCmd, DataAccess.ResultType.DataSet); employeeData = ds.GetXml ds.Dispose(); } catch (Exception ex) { employeeData = "Error: " + "Getting Employees [GetEmployees]" + Environment.NewLine + "Details: " + Environment.NewLine + ex.Message; } return employeeData; } } }
DataAccess.cs
using System; using System.Collections; using System.Configuration; using System.Data; using System.Data.OracleClient; namespace MyApplication.App_Code { public class DataAccess { private OracleConnection oraConn; private String connString; public enum ResultType { DataReader = 0, DataSet = 1, DataTable = 2 } public DataAccess() { connString = System.Configuration.ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString; } public object ExecuteSQLCommand(OracleCommand oraCommand, ResultType ReturnType, string TableName = "") { OracleDataAdapter oraDataAdapter = new OracleDataAdapter(oraCommand); oraConn = new OracleConnection(sConnectionString); try { oraConn.Open(); oraCmd.Connection = oraConn; oraCmd.CommandType = CommandType.Text; switch (ReturnType) { case ResultType.DataReader: OracleDataReader oraDataReader = null; oraDataReader = oraCmd.ExecuteReader(); return oraDataReader; case ResultType.DataSet: DataSet ds = new DataSet(); oDataAdapter.Fill(ds); oraConn.Close(); oraConn.Dispose(); return ds; case ResultType.DataTable: DataTable dt = new DataTable(); if (!string.IsNullOrEmpty(TableName)) dt.TableName = TableName; oDataAdapter.Fill(dt); oraConn.Close(); oraConn.Dispose(); return dt; } } catch (OracleException oException) { throw oException; } finally { oDataAdapter.Dispose(); oDataAdapter = null; oraCmd.Dispose(); } return null; } public int ExecuteSQLNonQueryCommand(OracleCommand oraCommand) {
The above code is pretty clear. Call the web service and receive the received data in XML format. To execute commands without a request, simply replace the command line passed in the command object and call the required method in the DataAccess.cs class.
I am already stunned by different opinions about why, at least, the above should be avoided and instead call for RESTful type calls. But I have not seen anything that at least helps to transform this code to at least cover the RESTful architecture.
I am sure many people use this (remember, I am still using a lot of this currently), based on the reason that:
- He works.
- Ease of implementation, support and management.
- There are so many people running the database in SQL commands that the ability to use these SQL chops that we use in the SQL editor is easy in the application - this is a big sigh of relief. Why would you use ORM when you can simply implement all your multiple queries (including stored procedures) using the example above.
- Most of the code samples available for data-related applications show the same pattern as above (the data set is populated from the Command object and returned as a DataSet or XML, etc.).
For someone to agree on what people call "best practice" in this area of ββcoding, you need to show why it is better and how much easier it is to do such a thing over what has been tried and tested for work.
If I can ask our fellow experts to show me how to convert them, and some explanation of why converting to REST would be better (via code), then I would be more than grateful for that.
Rate your entries. Thanks.
Additionally: I just want to note that although this is correct, I have some doubts that this approach is best after reading this article:
http://www.codeproject.com/Feature/WeirdAndWonderful.aspx?msg=4324770#xx4324770xx
In the article above, as I commented on the above, βI found this in the web service that I am updating. Itβs hard for me to find something wrong with this.β
I am also trying to establish what is REALLY wrong with this, as I am in a binding.
Let me give you some situations:
- The Client / Client asks you for an application that requests information stored in the database.
- Come up with a solution using the method described above. Customer requirements are provided. The solution is reliable, fast and convenient.
So, in essence, another question I want to ask is what is really wrong with the code above?