Note that a slightly different argument applies to non-HTML formats, e.g. a WPF app; but it's the same conclusion in the end. Here, at the end of a rendering a view the entity does not necessarily descope yet (because it can and most likely will be kept in memory). However, the only way in which your rendered view impacts the entity is if you set up a binding. And it makes no sense to set up the binding for the content of an immutable object. If it's immutable, then you wouldn't be able to reflect any changes made to the data anyway.
var model = new CustomerOrderPageViewModel(new Customer());
model.CustomerSubject.Name = "I can totally change this";
Is this immutable? I've heard argument either way. You cannot alter a CustomerOrderPageViewModel
instance., so it must be immutable, right?
However, it's still possible to indirectly alter the data contained in this CustomerOrderPageViewModel
instance:
var customer = new Customer() { Name = "Original" };
var model = new CustomerOrderPageViewModel(customer);
var modelName_1 = model.CustomerName;
customer.Name = "Different";
var modelName_2 = model.CustomerName;
var isImmutable = modelName_1 == modelName_2; // false
Note that when you do actually implement the indirection described above, and your viewmodels and entities are separate, then you can in fact achieve immutability easily here. I just wanted to point out that what you call immutability is not in fact immutability, because otherwise you are liable to repeat that mistake even if you were to add a layer of indirection.