【问题标题】:Service Stack IRequiresHttpRequest Pattern服务栈 IRequiresHttpRequest 模式
【发布时间】: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


    【解决方案1】:

    您阅读了哪些有关IRequiresHttpRequest 的文档? IRequiresHttpRequest 与IRequiresRequestContext 的工作方式相同,仅用于装饰 ServicesValidators 以告诉 ServiceStack 它需要访问并注入当前的 IHttpRequest 或 IRequestContext。

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

    【讨论】:

    • 那个页面只提到了IRequiresRequestContext,我肯定以为IRequiresHttpRequest会以同样的方式被注入...
    • 好的,但是这涉及在您的服务中注入请求上下文,而不是请求 DTO 和跳过反序列化。我的回答解释了这些差异。
    • 哦,我明白了,我误会了,我以为界面是 dtos 的
    • 所以如果我想用 HttpRequest 中的一些特定内容生成一个 dto,然后让 ServiceStack 自动映射其余部分,这可能吗?什么是推荐的为什么要实现我想要做的事情。 Ty寻求帮助:D
    猜你喜欢
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多