Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, it's definitely <em>not</em> immutable. The <code>registered</code> value changes over the lifetime of the object.</p> <p>To make it immutable, you mustn't allow the <code>registered</code> field to change over the lifetime of the object - instead, make <code>Register</code> return a new object which has a <code>true</code> value for <code>registered</code></p> <p>I'd also remove the private setter for <code>SomeProperty</code>, just having a getter and a readonly variable.</p> <p>So something like this:</p> <pre><code>public class NewClass { private readonly bool registered; private readonly string someProperty; public bool Registered { get { return registered; } } public string SomeProperty { get { return someProperty; } } public NewClass Register() { // Note the change of exception here if (registered) { throw new InvalidOperationException("Already registered"); } return new NewClass(somePropertyFromDB, true); } // You may want to have a public constructor with just someProperty // which calls this one, which you could make private public NewClass(string someProperty, bool registered) { this.registered = registered.; this.someProperty = someproperty; } } </code></pre> <p>Some notes:</p> <ul> <li>Another option would be to have two separate classes, <code>RegisteredFoo</code> and <code>UnregisteredFoo</code>; that might make it easier to understand code using this class</li> <li>There's nothing to stop someone calling <code>Register</code> twice, so making this immutable doesn't really help in terms of idempotency. As there's a natural side-effect (talking to the database) it's hard to make this truly functional.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload