先回归下SS的运行环境
我们接续前文,说明一下ServiceStack.Examples中的实用经典的代码(下面的代码是更新成新版写法后的):
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或者一组用户名为条件,搜索出一个用户列表。我们先看入口类的参数参数定义:
public class GetUsers
{
public List<long> UserIds { get; set; }
public List<string> UserNames { get; set; }
}
入口类参数定义了两个列表,
UserIds
为用户ID的一组列表 ,通过
dbConn.GetByIds<User>(request.UserIds)
查询到符合这组ID的用户列表, 再调用
users.AddRange
加入到出口类中的data属性上
UserNames
为用户名字的一组列表,通过
dbConn.Select<User>("UserName IN ({0})", request.UserNames.SqlInValues())
查询到一组包含有这组用户名的用户(是通过SQL的IN操作),再调用
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; }
}
出口类是有一个User实体类集合,加上一个操作相应状态类组成,原有出口类中用户列表使用的是Users属性(
this.Users = ArrayOfUser{get;set;}
),根据对接到extjs的要求,这个列表的属性要求名字为data,这里改为data,ArrayOfUser是一个用在旧版中的自定义的集合类,我们只需要使用List<User>即可,不需要定义这个集合
以下是User实体类:
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错误返回代码,错误消息以及错误堆栈等,而且提供了三种形式的覆写构造方式。
// 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
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。