Reputation: 16246
[Default]
data annotation works with ORMLite. However, it won't work with default values of a response. Is there anything similar to the [Default]
attribute that is for response DTO?
Considering the following code:
[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; }
}
I want Response DTO Pong property to have a default value "(nothing comes back!)" instead of just null.
Upvotes: 1
Views: 857
Reputation: 59377
Just set it in the constructor. DTOs in ServiceStack are plain C# objects. Nothing special.
public class HelloResponse
{
public HelloResponse()
{
this.Pong = "(nothing comes back!)";
}
public ResponseStatus ResponseStatus { get; set; }
public string Pong { get; set; }
}
The constructor for a class will always run before any properties set in an object initializer:
var resp = new HelloResponse();
Console.WriteLine(resp.Pong); // "(nothing comes back!)"
resp = new HelloResponse
{
Pong = "Foobar";
};
Console.WriteLine(resp.Pong); // "Foobar"
Upvotes: 6