2013-04-09 109 views
1

这是如何工作的?我已阅读文档,但希望获得更多信息。服务堆栈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映射器吗?

回答

3

您阅读了关于IRequiresHttpRequest的哪些文档? IRequiresHttpRequest与IRequiresRequestContext的作用相同,仅用于装饰服务验证程序告诉ServiceStack它需要访问并注入当前的IHttpRequest或IRequestContext。

Custom Serialization/Deserialization wiki的仅提到IRequiresRequestStreamIRequiresSoapMessage可以请求的DTO用于发信号到ServiceStack跳过处理所述请求体,并允许您自己手动反序列化请求。

+0

https://github.com/ServiceStack/ServiceStack/wiki/Access-HTTP-specific-features-in-services – sungiant 2013-04-09 14:01:38

+0

该页面只提到IRequiresRequestContext,我必须假设IRequiresHttpRequest将以相同的方式注入。 – sungiant 2013-04-09 14:02:46

+0

没问题,但是会谈到在您的服务中注入请求上下文,而不是请求DTO和跳过反序列化。我的回答解释了差异。 – mythz 2013-04-09 14:09:40