Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change (req, res) pair to a single http context object #1292

Closed
hacksparrow opened this issue Apr 27, 2018 · 6 comments
Closed

Change (req, res) pair to a single http context object #1292

hacksparrow opened this issue Apr 27, 2018 · 6 comments
Assignees
Labels
Milestone

Comments

@hacksparrow
Copy link
Contributor

hacksparrow commented Apr 27, 2018

Description / Steps to reproduce / Feature proposal

Use a single object object to contain the request and response object.

Current Behavior

(req, res)

Expected Behavior

(httpContext)

Acceptance Criteria

(req, res ...) should be replaced with (httpContext ...).

See Reporting Issues for more tips on writing good issues

@hacksparrow hacksparrow self-assigned this Apr 27, 2018
@dhmlau dhmlau added the DP3 label Apr 27, 2018
@hacksparrow hacksparrow changed the title Changed (req, res) pair to a single http context object Change (req, res) pair to a single http context object May 3, 2018
@bajtos
Copy link
Member

bajtos commented May 3, 2018

We should consider a different name than httpContext - I am worried "context" is to easy to confuse with IoC Context class. See #1082 (comment):

Using the name "context" for the object containing HTTP request/response is confusing, because we also have Context class with app-level and request-level instances. Let's find a better, less ambiguous name please!

@bajtos
Copy link
Member

bajtos commented May 4, 2018

Cross-posting from a private chat:

Any suggestion for the { req, res } object name?

Hmm, not really 😞 Maybe HttpFrame or HttpRequestFrame? It is a bit unusual though.

As I am thinking about this problem, I am leaning towards the conclusion that the problem is in the very fact that we have two request-related contexts: one holding req/res objects, another an instance of Context.

What if we reworked our design so that there is only a single http-request-related context object?

For example:

class HttpRequestContext extends Context {
  constructor(
    parentContext: Context, 
    public readonly request: ExpressRequest,
    public readonly response: ExpressResponse,
    // add any other required args/properties as needed) {
  ) {
    super(parentContext);
    this.bind(RestBindings.Http.REQUEST).to(this.request).lock();
    this.bind(RestBindings.Http.RESPONSE).to(this.response).lock();
    //...
  }
}

The upside is that we remove any possible confusion about what kind of an object we are receiving as a context and any code working at this low level automatically receive access to the IoC context associated with the incoming request.

The potential downside is that we make it easier for low-level HTTP code to bypass dependency injection via @inject and use the context as a Service Locator, which is sort of an anti-pattern. Having said that, one can always get hold of the current context via @inject.context(), thus my proposal will not make things much worse.

@raymondfeng @hacksparrow thoughts?

@hacksparrow
Copy link
Contributor Author

Would like to hear from @raymondfeng.

@raymondfeng
Copy link
Contributor

I like @bajtos's proposal to consolidate the http req/res wrapper object with our IoC Context. But I do have some concerns too:

  1. HttpRequestContext becomes more complex that just a wrapper of req/res.
  2. The construction of HttpRequestContext needs collaboration from LB and the underlying http framework. In my original PoC, I expect the http framework to create the HttpContext.
  3. We can use getter function for req/res so that we can always get the bound req/res.

@bajtos
Copy link
Member

bajtos commented May 10, 2018

I do have some concerns too.

Responded in my pull request, let's keep the discussion there for now: #1316 (comment)

@bajtos
Copy link
Member

bajtos commented May 15, 2018

Closing as done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants