【发布时间】:2013-04-09 12:41:20
【问题描述】:
这是如何工作的?我已阅读文档,但希望获得更多信息。
通过阅读文档,我了解到当我的 DTO 实现 IRequiresHttpRequest 时,DTO 的属性将不会自动填充,但在我的 DTO 中,我现在可以访问 HttpRequest 对象,因此我可以将我的 DTO 更改为具有“获取”属性从请求对象中提取内容。
将 HttpRequest 注入我的 DTO 意味着什么?文档建议服务堆栈在幕后执行此操作,但是只有注册自定义请求绑定器并手动注入 HttpRequest 对象才能使其工作。
RequestBinders.Add(typeof(MyDto), httpReq => {
var dto = new MyDto();
dto.HttpRequest = httpReq;
return dto;
});
问题 1:IRequiresHttpRequest 的注入究竟是如何工作的?
问题 2:有没有办法获得对 HttpRequest 对象的访问权限,以便我的 DTO 可以支持自定义“获取”属性,仍然让服务堆栈运行它自动映射?例如:
public class MyDto
: IRequiresHttpRequest
{
public Int32 AutoMappedProperty1 { get; set; }
public Int32 AutoMappedProperty2 { get; set; }
public Int32 AutoMappedProperty3 { get; set; }
public Int32 AutoMappedProperty4 { get; set; }
public Int32 CustomMappedProperty { get { return customMappedProperty; } }
IHttpRequest httpRequest;
public IHttpRequest HttpRequest
{
get
{
return httpRequest;
}
set
{
httpRequest = value;
// lets say this searches the query string for a variety of
// different keys, and then maps one of them of
// CustomMappedProperty based upon a specific set of rules
customMappedProperty = [...]
}
}
}
在上面的例子中,我定义了如何填充 CustomMappedProperty,但我仍然希望服务堆栈继续并映射所有可“设置”的属性。有没有办法做到这一点?我可以手动调用服务堆栈 dto mapper 吗?
【问题讨论】:
标签: servicestack