Skip to content

Commit 6747dd3

Browse files
committed
2 parents 5af1941 + 9cc2692 commit 6747dd3

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

README.md

+20-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
[![NuGet package](https://img.shields.io/nuget/v/T4Immutable.svg)](https://nuget.org/packages/T4Immutable)
55
#### Release notes
6+
* **[v1.2.1]** Now supports generating ToBuilder() and a better OptParam implementation.
67
* **[v1.2.0]** Now supports generating builders.
78
* **[v1.2.0]** WithParam class is now called OptParam.
89
* **[v1.1.5]** Added a PreConstructor option to write code such as atributtes before generated constructors.
@@ -40,7 +41,11 @@ It will automatically generate for you in a separate partial class file the foll
4041
```c#
4142
var builder = new Person.Builder().With(firstName: "John").With(lastName: "Doe"); // fluid way
4243
builder.Age = 21; // or via properties
44+
// that can be read back
45+
string firstName = builder.FirstName; // "John"
46+
var lastName = builder.LastName.Value; // "Doe"
4347
Person johnDoe = b.Build();
48+
var janeDoe = johnDoe.ToBuilder().With(firstName: "Jane", age: 20).Build(); // back and forth
4449
```
4550

4651
## How do I start?
@@ -93,7 +98,8 @@ You sure can, just add to the ImmutableClass attribute something like this:
9398
ImmutableClassOptions.ExcludeToString | // do not generate a ToString() method
9499
ImmutableClassOptions.ExcludeWith | // do not generate a With() method
95100
ImmutableClassOptions.ExcludeConstructor | // do not generate a constructor
96-
ImmutableClassOptions.ExcludeBuilder | // do not generate a builder (usually used alongside ExcludeConstructor)
101+
ImmutableClassOptions.ExcludeBuilder | // do not generate a builder or ImmutableToBuilder() - implies ExcludeToBuilder (usually used alongside ExcludeConstructor)
102+
ImmutableClassOptions.ExcludeToBuilder | // do not generate a builder or ToBuilder() method
97103
ImmutableClassOptions.AllowCustomConstructors)] // allow custom constructors
98104
```
99105
Note that even if you exclude for example the `Equals()` method implementation you can still use them internally by invoking the `private bool ImmutableEquals(...)` implementation. This is done in case you might want to write your own `Equals()` yet still use the generated one as a base.
@@ -285,12 +291,21 @@ partial class Person : IEquatable<Person> {
285291
return ImmutableWith(firstName, lastName, age);
286292
}
287293

294+
public Person.Builder ToBuilder() {
295+
return ImmutableToBuilder();
296+
}
297+
298+
private Person.Builder ImmutableToBuilder() {
299+
return new Person.Builder().With(
300+
new T4Immutable.OptParam<string>(this.FirstName),
301+
new T4Immutable.OptParam<string>(this.LastName),
302+
new T4Immutable.OptParam<int>(this.Age)
303+
);
304+
}
305+
288306
public class Builder {
289-
[T4Immutable.GeneratedCode, System.CodeDom.Compiler.GeneratedCode("T4Immutable", "1.2.0"), System.Diagnostics.DebuggerNonUserCode]
290307
public T4Immutable.OptParam<string> FirstName { get; set; }
291-
[T4Immutable.GeneratedCode, System.CodeDom.Compiler.GeneratedCode("T4Immutable", "1.2.0"), System.Diagnostics.DebuggerNonUserCode]
292308
public T4Immutable.OptParam<string> LastName { get; set; }
293-
[T4Immutable.GeneratedCode, System.CodeDom.Compiler.GeneratedCode("T4Immutable", "1.2.0"), System.Diagnostics.DebuggerNonUserCode]
294309
public T4Immutable.OptParam<int> Age { get; set; }
295310

296311
public Builder With(T4Immutable.OptParam<string> firstName = default(T4Immutable.OptParam<string>), T4Immutable.OptParam<string> lastName = default(T4Immutable.OptParam<string>), T4Immutable.OptParam<int> age = default(T4Immutable.OptParam<int>)) {
@@ -307,7 +322,6 @@ partial class Person : IEquatable<Person> {
307322
if (!this.Age.HasValue) throw new InvalidOperationException("Builder property 'Age' cannot be left unassigned");
308323
return new Person(this.FirstName.Value, this.LastName.Value, this.Age.Value);
309324
}
310-
}
311-
325+
}
312326
}
313327
```

0 commit comments

Comments
 (0)