Simple Immutable objects
I'm currently at #monospace and over lunch was discussing message passing with @ackenpacken and @briandonahue. I was lamenting that to create an immutable object in C# was just a lot harder than a mutable one. You either have to create a builder of sorts or create very verbose constructors and never mind trying to derive a new immutable instance with just one field changed. I suggested that if only private setters could be used with object initializers, this would be solved and realized that I could just use anonymous objects as my initializers and so after finishing with talks for the day I coded up a proof of concept.
// create a new data class
public class Person {
public int Id { get; private set; }
public string Name { get; private set; }
}
So this is a perfectly good immutable object. So good, we can't even initialize it. Enter Droog.ImMutie.Immutable
:
The argument to Immutable.Create
is simply object, so we can pass in an anonymous object with whatever properties we want. Create then provides and instance and copies over values on property name and type match. And if we want to change the Name?
// derive new instance with different name (using extension method)
person = person.With(new {Name = "Robert"});
With let's you provide a new anonymous object, will clone the previous instance and then overwrite each newly provided field by name/type match.
Very simple, no error checking, not the most efficient code, but it works and you can get it from github :)