先回归下SS的运行环境

我们接续前文,说明一下ServiceStack.Examples中的实用经典的代码(下面的代码是更新成新版写法后的):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 }; } |
这段服务实现的功能是通过一组ID或者一组用户名为条件,搜索出一个用户列表。我们先看入口类的参数参数定义:
1 2 3 4 5 | public class GetUsers { public List< long > UserIds { get ; set ; } public List< string > UserNames { get ; set ; } } |
入口类参数定义了两个列表,
为用户ID的一组列表 ,通过
1 | dbConn.GetByIds<User>(request.UserIds) |
查询到符合这组ID的用户列表, 再调用
加入到出口类中的data属性上
为用户名字的一组列表,通过
1 | dbConn.Select<User>( "UserName IN ({0})" , request.UserNames.SqlInValues()) |
查询到一组包含有这组用户名的用户(是通过SQL的IN操作),再调用
加入到出口类的data属性上
出口类的定义:
1 2 3 4 5 6 7 8 9 10 | 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 ; } } |
出口类是有一个User实体类集合,加上一个操作相应状态类组成,原有出口类中用户列表使用的是Users属性(
1 | this .Users = ArrayOfUser{ get ; set ;} |
),根据对接到extjs的要求,这个列表的属性要求名字为data,这里改为data,ArrayOfUser是一个用在旧版中的自定义的集合类,我们只需要使用List<User>即可,不需要定义这个集合
以下是User实体类:
1 2 3 4 5 6 7 8 9 | 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系统内置的HTTP相应状态类,其中封装了HTTP错误返回代码,错误消息以及错误堆栈等,而且提供了三种形式的覆写构造方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // 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后的项目代码
http://down.51cto.com/data/1964107