Closed anuraaga closed 6 years ago
Such a great idea. I totally agree with you especially for the part that removes the duplication and has same signature with the Service
.
Just a little bit worried about complaint from our customers though. :-)
+1 to the proposal as well. Less duplication without losing brevity is awesome. What do you think @imasahiro @kojilin and other users?
Sounds good to me~
Thanks for the comments, glad the new API seems reasonable. Want to confirm - are people ok with a full break of the API, or is a transition with something like AbstractHttpService2
a better idea? If we were 1.0, the latter would be required, and is a fairly common pattern in Java libraries, but as we're not, I would want to have buy-in from the users if we skipped a transition period.
Implemented the API change, somehow without any hard breaks :)
Currently we have a convenience abstract class,
AbstractHttpService
which takes care of HTTP request method dispatch to user service methods. The user methods have a signature likewhere
res
is always an instance ofDefaultHttpResponse
.I propose we consider changing these to not pass in a
HttpResponse
implementation and leave it to the user, i.e.,Reasons for this change are
Having
HttpResponseWriter
passed in isn't actually all that convenient.Consider a common pattern
is actually more lines than
and if the user actually does need a streaming response, it's just two lines to construct a streaming-type response and return it. I think in practice, passing in the writer rarely wins and often loses in terms of code complexity.
Provides more flexibility
As mentioned on #846, it is good to be able to select response implementation based on what the response actually is, often for performance reasons. For example, in the above examples all
HttpResponse.of
could be implemented using a non-streaming, optimized version ofHttpResponse
, without the user having to know the details. This allows us to experiment with implementations in the background without being tied to user code. All we needed was anHttpResponse
, but restricting this interface toHttpResponseWriter
cuts down on options, and as described above doesn't seem to have much benefit.Encourages good practices
One option would be to not make this change and expect users that want to be able to select
HttpResponse
implementation to implementHttpService
. But as method dispatch logic inAbstractHttpService
is quite useful in most cases, many users will probably continue to use it and miss out on any work we do onHttpResponse
. In particular, I suspect that most user code will be optimized if they switch fromHttpResponseWriter
toDeferredHttpResponse
+FixedHttpResponse
.Removes some duplication
Currently, we have this comment, and annoying duplication of APIs among many objects
If we promote the pattern that
HttpResponseWriter
is only really for streaming, not for full responses, then we can remove all therespond
methods from it, improving the situation a little.Possibly less cognitive load
This could just be me, but I have always find myself get tripped up a little by the method signature inconsistency between
AbstractHttpService
andDecoratingService
, both of which I commonly use.return
ing responses also feels more natural than writing a "pre-closed" response.We could make
HttpResponse
the general entry-point to creating responses by a pattern like this.Of course, this change is a pretty breaking change - the compatibility issue may by itself be a blocker. However, it might not be so bad.