diff --git a/README.md b/README.md index a98438f..f5455f5 100644 --- a/README.md +++ b/README.md @@ -14,72 +14,93 @@ https://nuget.org/packages/With.Fody/ ## Your Code - public class MyClass +``` cs +public class MyClass +{ + public MyClass(int intValue, string stringValue, OtherClass c1, OtherClass c2) { - public MyClass(int intValue, string stringValue, OtherClass c1, OtherClass c2) - { - this.IntValue = intValue; - this.StringValue = stringValue; - this.C1 = c1; - this.C2 = c2; - } + this.IntValue = intValue; + this.StringValue = stringValue; + this.C1 = c1; + this.C2 = c2; + } - public int IntValue { get; } + public int IntValue { get; } - public string StringValue { get; } + public string StringValue { get; } - public OtherClass C1 { get; } + public OtherClass C1 { get; } - public OtherClass C2 { get; } + public OtherClass C2 { get; } - // Needed for IntelliSense/Resharper support - public MyClass With(T value) => this; - // If two properties have same type, we need to append the property name to With - public MyClass WithC1(OtherClass value) => this; - public MyClass WithC2(OtherClass value) => this; - } + // Needed for IntelliSense/Resharper support + public MyClass With(T value) => this; + + // If two properties have same type, we need to append the property name to With + public MyClass WithC1(OtherClass value) => this; + public MyClass WithC2(OtherClass value) => this; + + // We can make an explicit version of With for multiple parameters + public MyClass With(int intValue, string stringValue) => this; + + // Method name can be more explicit, and parameter names should match property names + public MyClass WithC1andC2(OtherClass c1, OtherClass c2) => this; +} +``` ## What gets compiled - public class MyClass +``` cs +public class MyClass +{ + public MyClass(int intValue, string stringValue, OtherClass c1, OtherClass c2) + { + this.IntValue = intValue; + this.StringValue = stringValue; + this.C1 = c1; + this.C2 = c2; + } + + public int IntValue { get; } + + public string StringValue { get; } + + public OtherClass C1 { get; } + + public OtherClass C2 { get; } + + public MyClass With(int value) + { + return new MyClass(value, this.StringValue, this.C1, this.C2); + } + + public MyClass With(string value) + { + return new MyClass(this.IntValue, value, this.C1, this.C2); + } + + // If two properties have same type, we need to append the property name to With + public MyClass WithC1(OtherClass value) + { + return new MyClass(this.IntValue, this.StringValue, value, this.C2); + } + + public MyClass WithC2(OtherClass value) + { + return new MyClass(this.IntValue, this.StringValue, this.C1, value); + } + + public MyClass With(int intValue, string stringValue) + { + return new MyClass(intValue, stringValue, this.C1, this.C2); + } + + public MyClass WithC1andC2(OtherClass c1, OtherClass c2) { - public MyClass(int intValue, string stringValue, OtherClass c1, OtherClass c2) - { - this.IntValue = intValue; - this.StringValue = stringValue; - this.C1 = c1; - this.C2 = c2; - } - - public int IntValue { get; } - - public string StringValue { get; } - - public OtherClass C1 { get; } - - public OtherClass C2 { get; } - - public MyClass With(int value) - { - return new MyClass(value, this.StringValue, this.C1, this.C2); - } - - public MyClass With(string value) - { - return new MyClass(this.IntValue, value, this.C1, this.C2); - } - - // If two properties have same type, we need to append the property name to With - public MyClass WithC1(OtherClass value) - { - return new MyClass(this.IntValue, this.StringValue, value, this.C2); - } - - public MyClass WithC2(OtherClass value) - { - return new MyClass(this.IntValue, this.StringValue, this.C1, value); - } + return new MyClass(this.IntValue, this.StringValue, c1, c2); } +} +``` ## Motivation diff --git a/Tests/WeaverTests.cs b/Tests/WeaverTests.cs index 071ef0c..c07efb1 100644 --- a/Tests/WeaverTests.cs +++ b/Tests/WeaverTests.cs @@ -162,19 +162,19 @@ public void PrimitiveValues_ShortWithIsInjected() Assert.AreEqual(31231, result3.Value3); var result4 = instance.With(123, "World"); - Assert.AreEqual(123, result1.Value1); - Assert.AreEqual("World", result2.Value2); - Assert.AreEqual(31231, result3.Value3); + Assert.AreEqual(123, result4.Value1); + Assert.AreEqual("World", result4.Value2); + Assert.AreEqual(instance.Value3, result4.Value3); var result5 = instance.With(123, (long)31231); - Assert.AreEqual(123, result1.Value1); - Assert.AreEqual(instance.Value2, result3.Value2); - Assert.AreEqual(31231, result3.Value3); + Assert.AreEqual(123, result5.Value1); + Assert.AreEqual(instance.Value2, result5.Value2); + Assert.AreEqual(31231, result5.Value3); var result6 = instance.WithSecondAndThird("World", (long)31231); - Assert.AreEqual(instance.Value1, result2.Value1); - Assert.AreEqual("World", result2.Value2); - Assert.AreEqual(31231, result3.Value3); + Assert.AreEqual(instance.Value1, result6.Value1); + Assert.AreEqual("World", result6.Value2); + Assert.AreEqual(31231, result6.Value3); } [Test] @@ -199,14 +199,14 @@ public void PropertiesOfSameType_LongNamedWithIsInjected() Assert.AreEqual(333, result3.Value3); var result4 = instance.WithValue1Value2(111, 222); - Assert.AreEqual(111, result1.Value1); - Assert.AreEqual(222, result2.Value2); - Assert.AreEqual(instance.Value3, result1.Value3); + Assert.AreEqual(111, result4.Value1); + Assert.AreEqual(222, result4.Value2); + Assert.AreEqual(instance.Value3, result4.Value3); var result5 = instance.WithSecondAndThird(222, 333); - Assert.AreEqual(instance.Value1, result3.Value1); - Assert.AreEqual(222, result2.Value2); - Assert.AreEqual(333, result3.Value3); + Assert.AreEqual(instance.Value1, result5.Value1); + Assert.AreEqual(222, result5.Value2); + Assert.AreEqual(333, result5.Value3); } [Test]