Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Personally, I'm not really aware of any third party or previous solutions to this problem, so my apologies if I'm covering old ground. But, if I were going to implement some kind of immutability standard for a project I was working on, I would start with something like this:</p> <pre><code>public interface ISnaphot&lt;T&gt; { T TakeSnapshot(); } public class Immutable&lt;T&gt; where T : ISnaphot&lt;T&gt; { private readonly T _item; public T Copy { get { return _item.TakeSnapshot(); } } public Immutable(T item) { _item = item.TakeSnapshot(); } } </code></pre> <p>This interface would be implemented something like:</p> <pre><code>public class Customer : ISnaphot&lt;Customer&gt; { public string Name { get; set; } private List&lt;string&gt; _creditCardNumbers = new List&lt;string&gt;(); public List&lt;string&gt; CreditCardNumbers { get { return _creditCardNumbers; } set { _creditCardNumbers = value; } } public Customer TakeSnapshot() { return new Customer() { Name = this.Name, CreditCardNumbers = new List&lt;string&gt;(this.CreditCardNumbers) }; } } </code></pre> <p>And client code would be something like: </p> <pre><code> public void Example() { var myCustomer = new Customer() { Name = "Erik";} var myImmutableCustomer = new Immutable&lt;Customer&gt;(myCustomer); myCustomer.Name = null; myCustomer.CreditCardNumbers = null; //These guys do not throw exceptions Console.WriteLine(myImmutableCustomer.Copy.Name.Length); Console.WriteLine("Credit card count: " + myImmutableCustomer.Copy.CreditCardNumbers.Count); } </code></pre> <p>The glaring deficiency is that the implementation is only as good as the client of <code>ISnapshot</code>'s implementation of <code>TakeSnapshot</code>, but at least it would standardize things and you'd know where to go searching if you had issues related to questionable mutability. The burden would also be on potential implementors to recognize whether or not they could provide snapshot immutability and not implement the interface, if not (i.e. the class returns a reference to a field that does not support any kind of clone/copy and thus cannot be snapshot-ed).</p> <p>As I said, this is a start—how I'd probably start—certainly not an optimal solution or a finished, polished idea. From here, I'd see how my usage evolved and modify this approach accordingly. But, at least here I'd know that I could define how to make something immutable and write unit tests to assure myself that it was.</p> <p>I realize that this isn't far removed from just implementing an object copy, but it standardizes copy vis a vis immutability. In a code base, you might see some implementors of <code>ICloneable</code>, some copy constructors, and some explicit copy methods, perhaps even in the same class. Defining something like this tells you that the intention is specifically related to immutability—I want a snapshot as opposed to a duplicate object because I happen to want <em>n</em> more of that object. The <code>Immtuable&lt;T&gt;</code> class also centralizes the relationship between immutability and copies; if you later want to optimize somehow, like caching the snapshot until dirty, you needn't do it in all implementors of copying logic.</p>
    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. 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