2014-02-17 54 views
1

我是ServiceStack的新手,我目前不熟悉如何開發良好API的結構。我正嘗試使用相同的玩家模型做2件獨立的事情:ServiceStack對多個服務使用相同的「模型」

//POSTS SECTION 
    [Route("/getplayernamechanged", "POST")] 
    [Route("/getplayernamechanged2", "POST")] //I want to use this route to return a Player name concat with the string "just another test" 
    public class Player 
    { 
     public string Name { get; set; } 
    } 

    public class PlayerResponse 
    { 
     public string Name { get; set; } 
     public ResponseStatus ResponseStatus { get; set; } 
    } 

    public class PlayerService : Service 
    { 
     public object Any(Player request) 
     { 
      return new PlayerResponse { Name = request.Name+" just a test"}; 
     } 
    } 

我該怎麼做? 謝謝

回答

3

您需要定義兩個不同的請求對象,以便ServiceStack可以區分這兩個請求。

​​

然後,你需要做單獨行動的方法來處理每一個案件:

public class PlayerService : Service 
{ 
    // Route 1 handled here 
    public PlayerResponse Post(ChangePlayerNameRequest request) 
    { 
     return new PlayerResponse { Name = request.Name + " something else" }; 
    } 

    // Route 2 handled here 
    public PlayerResponse Post(ChangePlayerNameAndConcatRequest request) 
    { 
     return new PlayerResponse { Name = request.Name + " just a test"}; 
    } 
} 

注意我已經設置了返回類型爲PlayerResponse和對DTO的定義IReturn<PlayerResponse>。這將確保您的元數據是正確的。希望這有助於,讓我知道你是否在評論中發表意見。


如果您要使用請求對象相同Player模式,那麼你可以從Player擴展:

public class Person 
{ 
    public string Name { get; set; } 
} 

// Route 1 
[Route("/getplayernamechanged", "POST")] 
public class ChangePlayerNameRequest : Player, IReturn<PlayerResponse> 
{ 
} 

// Route 2 
[Route("/getplayernamechanged2", "POST")] 
public class ChangePlayerNameAndConcatRequest : Player, IReturn<PlayerResponse> 
{ 
} 

但最好避免繼承中的DTO在可能的情況。

+0

非常感謝。這就是我一直在尋找的! –

+0

@JoãoMartins不客氣。祝你的項目好運。 – Scott

相關問題