Disclaimer: I have not read the DDD bible so my understanding on Entities, Value Objects and Identities is derived from discussions on the web. But since “a rose by any other name smells as sweet” I might be right.
I haven’t got a compeling argument for the necessity of immutability of a request/response object. Still, I really want one and before I ask anybody for one I think I should present my premises for the arguments I made against the immutability. Sometimes we get the wrong conclusion not because the logic is flawed but because the premises are wrong. So, let’s get started.
Most of the articles I’ve read about Entities and Value Objects use a Person as an example of an entity and an Address as an example of a value-object. The reasoning is simple: a person can change its name but still, is the same person while an address that changes its street name is not the same address anymore. Everything is fine until you start to ask some questions. If a person’s birthdate is changed can we still talk about the same person? If an address represents “My home”, changing my home address did nothing to the identity of “My home”.
From my point of view we assign identities to value objects and by that we make them entities. A person is an entity because we decided this apriori based on how we think about the concept of “person”. We know that a person can change it’s name, social security number, parents etc and is still the same person because we recognize it but a different set of characteristics (visual appearance?). There is a mental disorder called prosopagnosia where people cannot recognize the face of other people. For the person that suffers from this disorder each person is a Value Object; person X might have the “proper” attributes but there is no identity associated with that person. One of these people was sure that his wife is an impostor; she looked like his wife but it was not recognized as his wife.
It is we who decide what object has an identity and what doesn’t. Let’s consider an ecommerce website where the user can manage a list of shipping addresses. If I want to the first address of addresses list one can say that I’m creating a new address while removing the old address from the system. On the other hand I am actually editing “the first address on my addresses list” which is… the identity of that address. Whatever changes I make to “the first address on the list”, that address will remain “the first address on my list”.
It is us who determine if an object has an identity or not. And in the case of the request handled by the a web application, the identity of that request is “the request sent by the client”. Just like I can still recognize my mother that suffered changes in name, appearance, age, location, weight and height I can still recognize “the request sent by the client” that moves through the application and was processed by a dispatcher and altered by some middleware, plugins etc. as well as “the response to be sent to the client” to which different components attach and change headers, completely transforming it from the time it was constructed until it reaches the user’s browser.