Skip to content

Commit 4a5e7bd

Browse files
committed
Value object ctor visibility can be changed via "ConstructorAccessModifier"
1 parent 6c704ee commit 4a5e7bd

File tree

12 files changed

+76
-4
lines changed

12 files changed

+76
-4
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Copyright>(c) $([System.DateTime]::Now.Year), Pawel Gerr. All rights reserved.</Copyright>
5-
<VersionPrefix>7.0.0</VersionPrefix>
5+
<VersionPrefix>7.1.0</VersionPrefix>
66
<Authors>Pawel Gerr</Authors>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageProjectUrl>https://github.com/PawelGerr/Thinktecture.Runtime.Extensions</PackageProjectUrl>

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static class ValueObject
1212
{
1313
public const ValueObjectAccessModifier DEFAULT_KEY_MEMBER_ACCESS_MODIFIER = ValueObjectAccessModifier.Private;
1414
public const ValueObjectMemberKind DEFAULT_KEY_MEMBER_KIND = ValueObjectMemberKind.Field;
15+
public const ValueObjectAccessModifier DEFAULT_CONSTRUCTOR_ACCESS_MODIFIER = ValueObjectAccessModifier.Private;
1516
}
1617

1718
public static class Modules
@@ -50,6 +51,7 @@ public static class Properties
5051
public const string SKIP_KEY_MEMBER = "SkipKeyMember";
5152
public const string KEY_MEMBER_ACCESS_MODIFIER = "KeyMemberAccessModifier";
5253
public const string KEY_MEMBER_KIND = "KeyMemberKind";
54+
public const string CONSTRUCTOR_ACCESS_MODIFIER = "ConstructorAccessModifier";
5355
}
5456

5557
public static class ValueObject

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/AllValueObjectSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public sealed class AllValueObjectSettings : IEquatable<AllValueObjectSettings>,
77
public string KeyMemberName { get; }
88
public bool SkipKeyMember { get; }
99
public bool SkipFactoryMethods { get; }
10+
public ValueObjectAccessModifier ConstructorAccessModifier { get; }
1011
public string CreateFactoryMethodName { get; }
1112
public string TryCreateFactoryMethodName { get; }
1213
public bool EmptyStringInFactoryMethodsYieldsNull { get; }
@@ -30,6 +31,7 @@ public AllValueObjectSettings(AttributeData valueObjectAttribute)
3031
KeyMemberName = valueObjectAttribute.FindKeyMemberName() ?? Helper.GetDefaultValueObjectKeyMemberName(KeyMemberAccessModifier, KeyMemberKind);
3132
SkipKeyMember = valueObjectAttribute.FindSkipKeyMember() ?? false;
3233
SkipFactoryMethods = valueObjectAttribute.FindSkipFactoryMethods() ?? false;
34+
ConstructorAccessModifier = valueObjectAttribute.FindConstructorAccessModifier() ?? Constants.ValueObject.DEFAULT_CONSTRUCTOR_ACCESS_MODIFIER;
3335
CreateFactoryMethodName = valueObjectAttribute.FindCreateFactoryMethodName() ?? "Create";
3436
TryCreateFactoryMethodName = valueObjectAttribute.FindTryCreateFactoryMethodName() ?? "TryCreate";
3537
EmptyStringInFactoryMethodsYieldsNull = valueObjectAttribute.FindEmptyStringInFactoryMethodsYieldsNull() ?? false;
@@ -68,6 +70,7 @@ public bool Equals(AllValueObjectSettings? other)
6870
&& KeyMemberName == other.KeyMemberName
6971
&& SkipKeyMember == other.SkipKeyMember
7072
&& SkipFactoryMethods == other.SkipFactoryMethods
73+
&& ConstructorAccessModifier == other.ConstructorAccessModifier
7174
&& CreateFactoryMethodName == other.CreateFactoryMethodName
7275
&& TryCreateFactoryMethodName == other.TryCreateFactoryMethodName
7376
&& EmptyStringInFactoryMethodsYieldsNull == other.EmptyStringInFactoryMethodsYieldsNull
@@ -94,6 +97,7 @@ public override int GetHashCode()
9497
hashCode = (hashCode * 397) ^ KeyMemberName.GetHashCode();
9598
hashCode = (hashCode * 397) ^ SkipKeyMember.GetHashCode();
9699
hashCode = (hashCode * 397) ^ SkipFactoryMethods.GetHashCode();
100+
hashCode = (hashCode * 397) ^ ConstructorAccessModifier.GetHashCode();
97101
hashCode = (hashCode * 397) ^ CreateFactoryMethodName.GetHashCode();
98102
hashCode = (hashCode * 397) ^ TryCreateFactoryMethodName.GetHashCode();
99103
hashCode = (hashCode * 397) ^ EmptyStringInFactoryMethodsYieldsNull.GetHashCode();

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/ComplexValueObjectCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ private void GenerateConstructor()
326326

327327
_sb.Append(@"
328328
329-
private ").Append(_state.Name).Append("(");
329+
").RenderAccessModifier(_state.Settings.ConstructorAccessModifier).Append(" ").Append(_state.Name).Append("(");
330330

331331
_sb.RenderArgumentsWithType(fieldsAndProperties);
332332

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/KeyedValueObjectCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ private void GenerateConstructor()
434434
{
435435
_sb.Append(@"
436436
437-
private ").Append(_state.Name).Append("(").RenderArgumentWithType(_state.KeyMember).Append(@")
437+
").RenderAccessModifier(_state.Settings.ConstructorAccessModifier).Append(" ").Append(_state.Name).Append("(").RenderArgumentWithType(_state.KeyMember).Append(@")
438438
{
439439
ValidateConstructorArguments(").RenderArgument(_state.KeyMember, "ref ").Append(@");
440440

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/ValueObjectSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Thinktecture.CodeAnalysis.ValueObjects;
66
private readonly AttributeInfo _attributeInfo;
77

88
public bool SkipFactoryMethods => _allSettings.SkipFactoryMethods;
9+
public ValueObjectAccessModifier ConstructorAccessModifier => _allSettings.ConstructorAccessModifier;
910
public bool SkipKeyMember => _allSettings.SkipKeyMember;
1011
public string CreateFactoryMethodName => _allSettings.CreateFactoryMethodName;
1112
public string TryCreateFactoryMethodName => _allSettings.TryCreateFactoryMethodName;
@@ -28,6 +29,7 @@ public ValueObjectSettings(
2829
public bool Equals(ValueObjectSettings other)
2930
{
3031
return SkipFactoryMethods == other.SkipFactoryMethods
32+
&& ConstructorAccessModifier == other.ConstructorAccessModifier
3133
&& SkipKeyMember == other.SkipKeyMember
3234
&& CreateFactoryMethodName == other.CreateFactoryMethodName
3335
&& TryCreateFactoryMethodName == other.TryCreateFactoryMethodName
@@ -50,6 +52,7 @@ public override int GetHashCode()
5052
unchecked
5153
{
5254
var hashCode = SkipFactoryMethods.GetHashCode();
55+
hashCode = (hashCode * 397) ^ (int)ConstructorAccessModifier;
5356
hashCode = (hashCode * 397) ^ SkipKeyMember.GetHashCode();
5457
hashCode = (hashCode * 397) ^ CreateFactoryMethodName.GetHashCode();
5558
hashCode = (hashCode * 397) ^ TryCreateFactoryMethodName.GetHashCode();

src/Thinktecture.Runtime.Extensions.SourceGenerator/Extensions/AttributeDataExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ public static class AttributeDataExtensions
2121

2222
public static ValueObjectAccessModifier? FindKeyMemberAccessModifier(this AttributeData attributeData)
2323
{
24-
var modifier = (ValueObjectAccessModifier?)GetIntegerParameterValue(attributeData, Constants.Attributes.Properties.KEY_MEMBER_ACCESS_MODIFIER);
24+
return attributeData.FindAccessModifier(Constants.Attributes.Properties.KEY_MEMBER_ACCESS_MODIFIER);
25+
}
26+
27+
public static ValueObjectAccessModifier? FindConstructorAccessModifier(this AttributeData attributeData)
28+
{
29+
return attributeData.FindAccessModifier(Constants.Attributes.Properties.CONSTRUCTOR_ACCESS_MODIFIER);
30+
}
31+
32+
private static ValueObjectAccessModifier? FindAccessModifier(this AttributeData attributeData, string propertyName)
33+
{
34+
var modifier = (ValueObjectAccessModifier?)GetIntegerParameterValue(attributeData, propertyName);
2535

2636
if (modifier is null || !modifier.Value.IsValid())
2737
return null;

src/Thinktecture.Runtime.Extensions/ComplexValueObjectAttribute.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public sealed class ComplexValueObjectAttribute : Attribute
1313
/// </summary>
1414
public bool SkipFactoryMethods { get; set; }
1515

16+
/// <summary>
17+
/// Access modifier of the constructor.
18+
/// Default is <see cref="ValueObjectAccessModifier.Private"/>.
19+
/// </summary>
20+
public ValueObjectAccessModifier ConstructorAccessModifier { get; set; }
21+
1622
private string? _createFactoryMethodName;
1723

1824
/// <summary>
@@ -63,4 +69,12 @@ public string DefaultInstancePropertyName
6369
get => _defaultInstancePropertyName ?? "Empty";
6470
set => _defaultInstancePropertyName = value;
6571
}
72+
73+
/// <summary>
74+
/// Initializes new instance of <see cref="ComplexValueObjectAttribute"/>.
75+
/// </summary>
76+
public ComplexValueObjectAttribute()
77+
{
78+
ConstructorAccessModifier = ValueObjectAccessModifier.Private;
79+
}
6680
}

src/Thinktecture.Runtime.Extensions/ValueObjectAttribute.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public string KeyMemberName
5151
/// </summary>
5252
public bool SkipFactoryMethods { get; set; }
5353

54+
/// <summary>
55+
/// Access modifier of the constructor.
56+
/// Default is <see cref="ValueObjectAccessModifier.Private"/>.
57+
/// </summary>
58+
public ValueObjectAccessModifier ConstructorAccessModifier { get; set; }
59+
5460
private string? _createFactoryMethodName;
5561

5662
/// <summary>
@@ -215,5 +221,6 @@ public ValueObjectAttribute()
215221
{
216222
KeyMemberType = typeof(TKey);
217223
KeyMemberAccessModifier = ValueObjectAccessModifier.Private;
224+
ConstructorAccessModifier = ValueObjectAccessModifier.Private;
218225
}
219226
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Thinktecture.Runtime.Tests.TestValueObjects;
2+
3+
[ComplexValueObject(ConstructorAccessModifier = ValueObjectAccessModifier.Public)]
4+
public sealed partial class ComplexValueObjectWithPublicCtor
5+
{
6+
public decimal Lower { get; }
7+
public decimal Upper { get; }
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Thinktecture.Runtime.Tests.TestValueObjects;
2+
3+
[ValueObject<int>(ConstructorAccessModifier = ValueObjectAccessModifier.Public)]
4+
public sealed partial class KeyedValueObjectWithPublicCtor
5+
{
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Thinktecture.Runtime.Tests.TestValueObjects;
2+
3+
namespace Thinktecture.Runtime.Tests.ValueObjectTests;
4+
5+
public class ConstructorTests
6+
{
7+
[Fact]
8+
public void Keyed_value_object_with_public_ctor()
9+
{
10+
var obj = new KeyedValueObjectWithPublicCtor(42);
11+
}
12+
13+
[Fact]
14+
public void Complex_value_object_with_public_ctor()
15+
{
16+
var obj = new ComplexValueObjectWithPublicCtor(1, 2);
17+
}
18+
}

0 commit comments

Comments
 (0)