Skip to main content
1 of 2
Mike Nakis
  • 32.8k
  • 7
  • 83
  • 116

An http response can be thought of as immutable from the moment that it has been fully constructed and issued. Before that moment, it is generally used as a "builder" (see Builder Pattern) which gets passed around in order for various routines to add to it piecemeal all the information that constitutes the final response. So, during the lifetime of the response as a builder, it is by its very nature mutable.

Now, it is still possible to take a class which is mutable by nature and turn it into an immutable class, but what I would like to suggest to you is that although doing so might be a fabulous exercise on paper, (the entire series of Erik Lippert's articles on immutability was nothing short of fascinating,) it is not really something that you would want to be doing in practice with any non-trivial classes, because you end up with abominations precisely like the one you discovered. Plus, it is clearly not worth the effort, and it is stupenduously wasteful in terms of computing resources.

So, my suggestion would be that if you really like the idea of an immutable response, (I like it too,) then rename your current response to ResponseBuilder, let it be mutable, and when it is complete, create an immutable response out of it and discard the builder.

Mike Nakis
  • 32.8k
  • 7
  • 83
  • 116