您好,登錄后才能下訂單哦!
先回歸下SS的運(yùn)行環(huán)境
我們接續(xù)前文,說明一下ServiceStack.Examples中的實(shí)用經(jīng)典的代碼(下面的代碼是更新成新版寫法后的):
public object Any(GetUsers request) { using (var dbConn = ConnectionFactory.OpenDbConnection()) { var users = new List<User>(); if (request.UserIds != null && request.UserIds.Count > 0) { users.AddRange(dbConn.GetByIds<User>(request.UserIds)); } if (request.UserNames != null && request.UserNames.Count > 0) { users.AddRange(dbConn.Select<User>( "UserName IN ({0})", request.UserNames.SqlInValues())); } return new GetUsersResponse { data = users }; }
這段服務(wù)實(shí)現(xiàn)的功能是通過一組ID或者一組用戶名為條件,搜索出一個(gè)用戶列表。我們先看入口類的參數(shù)參數(shù)定義:
public class GetUsers { public List<long> UserIds { get; set; } public List<string> UserNames { get; set; } }
入口類參數(shù)定義了兩個(gè)列表,
UserIds
為用戶ID的一組列表 ,通過
dbConn.GetByIds<User>(request.UserIds)
查詢到符合這組ID的用戶列表, 再調(diào)用
users.AddRange
加入到出口類中的data屬性上
UserNames
為用戶名字的一組列表,通過
dbConn.Select<User>("UserName IN ({0})", request.UserNames.SqlInValues())
查詢到一組包含有這組用戶名的用戶(是通過SQL的IN操作),再調(diào)用
users.AddRange
加入到出口類的data屬性上
出口類的定義:
public class GetUsersResponse { public GetUsersResponse() { this.data = new List<User>(); this.ResponseStatus = new ResponseStatus(); } public List<User> data { get; set; } public ResponseStatus ResponseStatus { get; set; } }
出口類是有一個(gè)User實(shí)體類集合,加上一個(gè)操作相應(yīng)狀態(tài)類組成,原有出口類中用戶列表使用的是Users屬性(
this.Users = ArrayOfUser{get;set;}
),根據(jù)對(duì)接到extjs的要求,這個(gè)列表的屬性要求名字為data,這里改為data,ArrayOfUser是一個(gè)用在舊版中的自定義的集合類,我們只需要使用List<User>即可,不需要定義這個(gè)集合
以下是User實(shí)體類:
public class User { [AutoIncrement] public int Id { get; set; } public string UserName { get; set; } public string Email { get; set; } public string Password { get; set; } public Guid GlobalId { get; set; } }
ResponseStatus 是SS系統(tǒng)內(nèi)置的HTTP相應(yīng)狀態(tài)類,其中封裝了HTTP錯(cuò)誤返回代碼,錯(cuò)誤消息以及錯(cuò)誤堆棧等,而且提供了三種形式的覆寫構(gòu)造方式。
// Summary: // Common ResponseStatus class that should be present on all response DTO's [DataContract] public class ResponseStatus { // Summary: // Initializes a new instance of the ServiceStack.ServiceInterface.ServiceModel.ResponseStatus // class. A response status without an errorcode == success public ResponseStatus(); // // Summary: // Initializes a new instance of the ServiceStack.ServiceInterface.ServiceModel.ResponseStatus // class. A response status with an errorcode == failure public ResponseStatus(string errorCode); // // Summary: // Initializes a new instance of the ServiceStack.ServiceInterface.ServiceModel.ResponseStatus // class. A response status with an errorcode == failure public ResponseStatus(string errorCode, string message); // Summary: // Holds the custom ErrorCode enum if provided in ValidationException otherwise // will hold the name of the Exception type, e.g. typeof(Exception).Name A value // of non-null means the service encountered an error while processing the request. [DataMember(Order = 1)] public string ErrorCode { get; set; } // // Summary: // For multiple detailed validation errors. Can hold a specific error message // for each named field. [DataMember(Order = 4)] public List<ResponseError> Errors { get; set; } // // Summary: // A human friendly error message [DataMember(Order = 2)] public string Message { get; set; } // [DataMember(Order = 3)] public string StackTrace { get; set; } }
更新了使用新版ServiceStack后的項(xiàng)目代碼
http://down.51cto.com/data/1964107
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。