ServiceStack 项目实例 008 ServiceStack.Examples - 2

本文介绍ServiceStack中查询用户的示例代码,演示如何通过ID列表或用户名列表获取用户信息。涉及连接数据库、执行查询及返回响应等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   先回归下SS的运行环境 

     wKiom1STuCCRipP3AAJMjYAmyzo242.jpg

 

          我们接续前文,说明一下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 { getset; }

    public List<string> UserNames { getset; }

}

        入口类参数定义了两个列表,

1

UserIds

       为用户ID的一组列表 ,通过

1

dbConn.GetByIds<User>(request.UserIds)

查询到符合这组ID的用户列表, 再调用

1

users.AddRange

加入到出口类中的data属性上      

1

UserNames

       为用户名字的一组列表,通过

1

dbConn.Select<User>("UserName IN ({0})", request.UserNames.SqlInValues())

查询到一组包含有这组用户名的用户(是通过SQL的IN操作),再调用

1

users.AddRange

加入到出口类的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 { getset; }

    public ResponseStatus ResponseStatus { getset; }

}

      出口类是有一个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 { getset; }

    public string UserName { getset; }

    public string Email { getset; }

    public string Password { getset; }

    public Guid GlobalId { getset; }

}

    

     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 { getset; }

        //

        // Summary:

        //     For multiple detailed validation errors.  Can hold a specific error message

        //     for each named field.

        [DataMember(Order = 4)]

        public List<ResponseError> Errors { getset; }

        //

        // Summary:

        //     A human friendly error message

        [DataMember(Order = 2)]

        public string Message { getset; }

        //

        [DataMember(Order = 3)]

        public string StackTrace { getset; }

    }

 

更新了使用新版ServiceStack后的项目代码 

http://down.51cto.com/data/1964107 

转载于:https://my.oschina.net/dingliu/blog/754161

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值