.net - What is a "mostly complete" (im)mutability approach for C#? -
since immutability not baked c# degree f#, or framework (bcl) despite support in clr, what's complete solution (im)mutability c#?
my order of preference solution consisting of general patterns/principles compatible with
- a single open-source library few dependencies
- a small number of complementary/compatible open-source libraries
- something commercial
that
- covers lippert's kinds of immutability
- offers decent performance (that's vague know)
- supports serialization
- supports cloning/copying (deep/shallow/partial?)
- feels natural in scenarios such ddd, builder patterns, configuration, , threading
- provides immutable collections
i'd include patterns community might come don't fit in framework such expressing mutability intent through interfaces (where both clients shouldn't change , may want to change can through interfaces, , not backing class (yes, know isn't true immutability, sufficient):
public interface ix { int y{ get; } readonlycollection<string> z { get; } imutablex clone(); } public interface imutablex: ix { new int y{ get; set; } new icollection<string> z{ get; } // or ilist<string> } // no 1 should ahold of x directly internal class x: imutablex { public int y{ get; set; } icollection<string> imutablex.z { { return z; } } public readonlycollection<string> z { { return new readonlycollection<string>(z); } } public imutablex clone() { var c = memberwiseclone(); c.z = new list<string>(z); return c; } private ilist<string> z = new list<string>(); } // ... public void contriveexample(ix x) { if (x.y != 3 || x.z.count < 10) return; var c= x.clone(); c.y++; c.z.clear(); c.z.add("bye, off thread"); // ... }
would better solution use f# want true immutability?
Comments
Post a Comment