这是如何工作的?我已阅读文档,但希望获得更多信息。服务堆栈IRequiresHttpRequest模式
从阅读文档我明白,当我的DTO实现IRequiresHttpRequest,那么DTO的属性将不会自动填充,但在我的DTO中,我现在可以访问HttpRequest对象,所以我可以将我的DTO更改为'get'从请求对象中抽取事物的属性。
什么是注入HttpRequest到我的DTO?文档建议服务栈在后台执行此操作,但是我只能在注册自定义请求绑定并手动注入HttpRequest对象时才能使其工作。
RequestBinders.Add(typeof(MyDto), httpReq => {
var dto = new MyDto();
dto.HttpRequest = httpReq;
return dto;
});
问题1:IRequiresHttpRequest的注入究竟意味着什么工作?
问题2:有没有办法获得对HttpRequest对象的访问权限,以便我的DTO可以支持自定义的'get'属性,仍然有服务栈运行它自动映射?例如:
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如何被填充,但我还是想服务栈先走,并映射所有“set'-能性能。有没有办法做到这一点?我可以手动调用服务堆栈dto映射器吗?
https://github.com/ServiceStack/ServiceStack/wiki/Access-HTTP-specific-features-in-services – sungiant 2013-04-09 14:01:38
该页面只提到IRequiresRequestContext,我必须假设IRequiresHttpRequest将以相同的方式注入。 – sungiant 2013-04-09 14:02:46
没问题,但是会谈到在您的服务中注入请求上下文,而不是请求DTO和跳过反序列化。我的回答解释了差异。 – mythz 2013-04-09 14:09:40