2013-10-14 2 views
1

[Default] 데이터 주석은 ORMLite와 함께 작동합니다. 그러나 응답의 기본값은 작동하지 않습니다. 응답 DTO를위한 [Default] 속성과 비슷한 것이 있습니까? ServiceStack 응답 기본값

[Route("api/hello")] 
public class Hello { 
    public string Ping { get; set; } 
} 
public class HelloResponse { 
    public ResponseStatus ResponseStatus { get; set; } 
    [Default(typeof(string), "(nothing comes back!)")] 
    public string Pong { get; set; } 
} 

내가 기본값을 가지고 응답 DTO 퐁 속성을 원하는 "(아무것도 다시 온다!)"대신 단지 널 (null) :

다음 코드를 고려.

답변

6

그냥 생성자에서 설정하십시오. ServiceStack의 DTO는 일반 C# 개체입니다. 특별한 것은 없습니다.

public class HelloResponse 
{ 
    public HelloResponse() 
    { 
     this.Pong = "(nothing comes back!)"; 
    } 

    public ResponseStatus ResponseStatus { get; set; } 
    public string Pong { get; set; } 
} 

항상 객체 이니셜에 설정된 속성 전에 실행하는 클래스의 생성자 :

var resp = new HelloResponse(); 
Console.WriteLine(resp.Pong); // "(nothing comes back!)" 

resp = new HelloResponse 
{ 
    Pong = "Foobar"; 
}; 
Console.WriteLine(resp.Pong); // "Foobar"