2013-10-14 62 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傍属性有一个默认值“(没有回来!)”,而不仅仅是空。

回答

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"