服务栈IRequiresHttpRequest模式

时间:2013-04-09 12:41:20

标签: servicestack

这是如何工作的?我已阅读文档但希望获得更多信息。

通过阅读文档,我了解到当我的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可以支持自定义'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'-able属性。有没有办法实现这个目标?我可以手动调用服务堆栈dto mapper吗?

1 个答案:

答案 0 :(得分:3)

您阅读了哪些关于IRequiresHttpRequest的文档? IRequiresHttpRequest与IRequiresRequestContext的工作方式相同,仅用于在服务验证器上进行装饰,以告知ServiceStack它需要访问并注入当前的IHttpRequest或IRequestContext。 / p>

Custom Serialization / Deserialization wiki仅提及可以在请求DTO 上使用IRequiresRequestStreamIRequiresSoapMessage向ServiceStack发送信号以跳过处理请求正文并允许您自己手动反序列化请求。