Restfull Web API
Restfull Web API最明顯的特徵就是用唯一的 uri 識別資源的類型與個體。不同的 HTTP method 表達對資源的不同操作,差不多對應到 CRUD。基本上就是叫人們不要使用一堆亂七八糟的路徑或查詢參數傳遞資料或操作類型😂這個道理很容易懂:如果只使用HTTP GET與POST,那麼很多操作(例如刪除、修改)就不得不使用路徑、查詢參數、或者在JSON 資料內放置旗標來表示,這樣的寫法將缺乏自我表達性(很難讀的意思)。
01 public sealed class SapRfcFactory {
02 private static SapRfc singleton = null;
03
04 public static SapRfc GetInstance() {
05 // 在此處決定要使用 real 或 mock 物件
06 if (singleton = null)
07 singleton = new SapRfcMock();
08 //singleton = new SapRfcReal();
09
10 return singleton;
11 }
12
13 private SapRfcFactory() {}
14
15 // other codes...
16 }
17
18 // 定義 RFC 呼叫介面
19 public abstract class SapRfc {
20 // 可以在此成員存放共用參數
21 Dictionary<string, object> dicParams = new Dictionary<string, object>();
22
23 public abstract bool Logon();
24 public abstract void Logout();
25 }
26
27 public class SapRfcMock: SapRfc {
28 public override bool Logon() {
29 // 直接回傳 true
30 return true;
31 }
32
33 public override void Logout() {
34 // do nothing.
35 }
36 }
37
38 public class SapRfcReal: SapRfc {
39 public override bool Logon() {
40 // 進行實際的 RFC 登入...
41 }
42
43 public override void Logout() {
44 // 進行實際的 RFC 登出...
45 }
46 }
科學面的技術與工具只是專案成功的必要條件,而不是充分條件。專案到頭來還是人去執行的,所以最後的成敗還是在人員管理。
{-------------------------------------------------------------------------------
This file is generated by EntityBuilder v1.2
provided by Albert Dong, albertdongtw@gmail.com
The Template Engine is from:
http://sourceforge.net/projects/delphi-templeng/
--------------------------------------------------------------------------------
Class: User
Table: user
Author: XXX
Date: 2013/8/20 下午 07:06:39
Description:
-------------------------------------------------------------------------------}
unit uUser; interface
uses
Windows, Messages, SysUtils, Variants, Contnrs, Classes, DB, uEntity;
const
TBL_User = 'user'; type
{** * Class description.
*
* @author XXX, 2013/08/20 19:06:39. Created.
*}
TUser = class(TEntity) private // ------ Fields ------ FNo: string; FName: string; FNameEng: string; FEmail: string; FRecvCalNotify: integer; FRecvCalOverdue: integer; FIsAdmin: integer; protected // ------ Property Accessors ------ function GetNo: string; procedure SetNo(value: string); function GetName: string; procedure SetName(value: string); function GetNameEng: string; procedure SetNameEng(value: string); function GetEmail: string; procedure SetEmail(value: string); function GetRecvCalNotify: integer; procedure SetRecvCalNotify(value: integer); function GetRecvCalOverdue: integer; procedure SetRecvCalOverdue(value: integer); function GetIsAdmin: integer; procedure SetIsAdmin(value: integer); procedure FillData(DataSet: TDataSet); override; public // ------ Utility Class Methods ------ class function ListAll: TList; class procedure DeleteAll; constructor Create; override; destructor Destroy; override; function ToString: string; override; procedure Reload; procedure Insert; procedure Update; procedure Delete; published // ------ Properties ------ property No: string read GetNo write SetNo; property Name: string read GetName write SetName; property NameEng: string read GetNameEng write SetNameEng; property Email: string read GetEmail write SetEmail; property RecvCalNotify: integer read GetRecvCalNotify write SetRecvCalNotify; property RecvCalOverdue: integer read GetRecvCalOverdue write SetRecvCalOverdue; property IsAdmin: integer read GetIsAdmin write SetIsAdmin; end; implementation
uses
uToString;
{ TUser }
class function TUser.ListAll: TList; var
stmt: string; begin
stmt := 'SELECT * FROM ' + TBL_User; result := TEntity.ExecQueryForList(stmt, TUser);
end; class procedure TUser.DeleteAll; begin
ExecUpdate('DELETE FROM ' + TBL_User); end; constructor TUser.Create; begin
inherited Create; // TODO: Constructor... end; destructor TUser.Destroy; begin
// TODO: Destructor... inherited Destroy; end; (**
* Fill object properties with the specified dataset.
* Every subclass of TEntity inherits AsXXX() methods.
*
* @param DataSet the specified dataset.
*)
procedure TUser.FillData(DataSet: TDataSet); begin
if DataSet = nil then raise Exception.Create('Nil DataSet!'); if DataSet.IsEmpty or DataSet.Eof then Exit;
FNo := AsString(DataSet, 'no'); FName := AsString(DataSet, 'name'); FNameEng := AsString(DataSet, 'name_eng'); FEmail := AsString(DataSet, 'email'); FRecvCalNotify := AsInteger(DataSet, 'recv_cal_notify'); FRecvCalOverdue := AsInteger(DataSet, 'recv_cal_overdue'); FIsAdmin := AsInteger(DataSet, 'is_admin'); end; procedure TUser.Reload; var
stmt: string; begin
stmt := 'SELECT * FROM ' + TBL_User + ' WHERE 1 = 1 ' + ' AND no = ' + QuotedStr(No) ;
TEntity.Load(stmt, self);
end; procedure TUser.Insert; var
stmt: string; begin
stmt := 'INSERT INTO ' + TBL_User + ' (' + 'no' + ', name' + ', name_eng' + ', email' + ', recv_cal_notify' + ', recv_cal_overdue' + ', is_admin' + ') VALUES (' + QuotedStr(No)
+ ', ' + QuotedStr(Name) + ', ' + QuotedStr(NameEng) + ', ' + QuotedStr(Email) + ', ' + IntToStr(RecvCalNotify) + ', ' + IntToStr(RecvCalOverdue) + ', ' + IntToStr(IsAdmin) + ') ' ;
ExecUpdate(stmt);
end; procedure TUser.Update; var
stmt: string; begin
stmt := 'UPDATE ' + TBL_User + ' SET ' + 'no = ' + QuotedStr(No) + ', name = ' + QuotedStr(Name) + ', name_eng = ' + QuotedStr(NameEng) + ', email = ' + QuotedStr(Email) + ', recv_cal_notify = ' + IntToStr(RecvCalNotify) + ', recv_cal_overdue = ' + IntToStr(RecvCalOverdue) + ', is_admin = ' + IntToStr(IsAdmin) + ' WHERE 1 = 1 ' + ' AND no = ' + QuotedStr(No) ;
ExecUpdate(stmt);
end; procedure TUser.Delete; var
stmt: string; begin
stmt := 'DELETE FROM ' + TBL_User + ' WHERE 1 = 1 ' + ' AND no = ' + QuotedStr(No) ;
ExecUpdate(stmt);
end; function TUser.ToString: string; begin
TToStringBuilder.toString(self);
end; function TUser.GetNo: string; begin
result := FNo;
end; procedure TUser.SetNo(value: string); begin
if FNo <> value then begin FNo := value;
end; end; function TUser.GetName: string; begin
result := FName;
end; procedure TUser.SetName(value: string); begin
if FName <> value then begin FName := value;
end; end; function TUser.GetNameEng: string; begin
result := FNameEng;
end; procedure TUser.SetNameEng(value: string); begin
if FNameEng <> value then begin FNameEng := value;
end; end; function TUser.GetEmail: string; begin
result := FEmail;
end; procedure TUser.SetEmail(value: string); begin
if FEmail <> value then begin FEmail := value;
end; end; function TUser.GetRecvCalNotify: integer; begin
result := FRecvCalNotify;
end; procedure TUser.SetRecvCalNotify(value: integer); begin
if FRecvCalNotify <> value then begin FRecvCalNotify := value;
end; end; function TUser.GetRecvCalOverdue: integer; begin
result := FRecvCalOverdue;
end; procedure TUser.SetRecvCalOverdue(value: integer); begin
if FRecvCalOverdue <> value then begin FRecvCalOverdue := value;
end; end; function TUser.GetIsAdmin: integer; begin
result := FIsAdmin;
end; procedure TUser.SetIsAdmin(value: integer); begin
if FIsAdmin <> value then begin FIsAdmin := value;
end; end; initialization
// Register this entity class for dynamic loading. RegisterClass(TUser);
finalization
UnRegisterClass(TUser);
end. |