Skip to content

Commit b6a761f

Browse files
committed
♻️ Refactor properties to use auto-implemented getters and private setters
1 parent 775df01 commit b6a761f

File tree

6 files changed

+22
-35
lines changed

6 files changed

+22
-35
lines changed

Directory.Build.props

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,5 @@
1717
<!-- Disable "must have curly braces for if/else" -->
1818
<NoWarn>$(NoWarn);IDE0011</NoWarn>
1919

20-
<!-- Disable "auto property warning" TODO: Remove after fields are introduced -->
21-
<NoWarn>$(NoWarn);IDE0032</NoWarn>
22-
2320
</PropertyGroup>
2421
</Project>

src/Domain/Common/Base/Auditable.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,30 @@ public abstract class Auditable : IAuditable
99
public const int UpdatedByMaxLength = 128;
1010

1111
private const string SystemUser = "System";
12-
private string _createdBy = null!;
13-
private string? _updatedBy;
1412

1513
public DateTimeOffset CreatedAt { get; private set; }
1614

1715
public string CreatedBy
1816
{
19-
get => _createdBy;
17+
get;
2018
private set
2119
{
2220
ThrowIfNullOrWhiteSpace(value, nameof(CreatedBy));
2321
ThrowIfGreaterThan(value.Length, CreatedByMaxLength, nameof(CreatedBy));
24-
_createdBy = value;
22+
field = value;
2523
}
26-
}
24+
} = null!;
2725

2826
public DateTimeOffset? UpdatedAt { get; private set; }
2927

3028
public string? UpdatedBy
3129
{
32-
get => _updatedBy;
30+
get;
3331
private set
3432
{
3533
ThrowIfNullOrWhiteSpace(value, nameof(UpdatedBy));
3634
ThrowIfGreaterThan(value.Length, UpdatedByMaxLength, nameof(UpdatedBy));
37-
_updatedBy = value;
35+
field = value;
3836
}
3937
}
4038

src/Domain/Heroes/Hero.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,27 @@ public class Hero : AggregateRoot<HeroId>
1313

1414
private readonly List<Power> _powers = [];
1515

16-
private string _name = null!;
17-
private string _alias = null!;
18-
1916
public string Name
2017
{
21-
get => _name;
18+
get;
2219
set
2320
{
2421
ThrowIfNullOrWhiteSpace(value, nameof(Name));
2522
ThrowIfGreaterThan(value.Length, NameMaxLength, nameof(Name));
26-
_name = value;
23+
field = value;
2724
}
28-
}
25+
} = null!;
2926

3027
public string Alias
3128
{
32-
get => _alias;
29+
get;
3330
set
3431
{
3532
ThrowIfNullOrWhiteSpace(value, nameof(Alias));
3633
ThrowIfGreaterThan(value.Length, AliasMaxLength, nameof(Alias));
37-
_alias = value;
34+
field = value;
3835
}
39-
}
36+
} = null!;
4037

4138
public int PowerLevel { get; private set; }
4239
public TeamId? TeamId { get; private set; }

src/Domain/Heroes/Power.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,27 @@ public record Power : IValueObject
44
{
55
public const int NameMaxLength = 50;
66

7-
private string _name = null!;
8-
private int _powerLevel;
9-
107
// Private setters needed for EF
118
public string Name
129
{
13-
get => _name;
10+
get;
1411
private set
1512
{
1613
ThrowIfNullOrWhiteSpace(value, nameof(Name));
1714
ThrowIfGreaterThan(value.Length, NameMaxLength, nameof(Name));
18-
_name = value;
15+
field = value;
1916
}
20-
}
17+
} = null!;
2118

2219
// Private setters needed for EF
2320
public int PowerLevel
2421
{
25-
get => _powerLevel;
22+
get;
2623
private set
2724
{
2825
ThrowIfLessThan(value, 1, nameof(PowerLevel));
2926
ThrowIfGreaterThan(value, 10, nameof(PowerLevel));
30-
_powerLevel = value;
27+
field = value;
3128
}
3229
}
3330

src/Domain/Teams/Mission.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66

77
public class Mission : Entity<MissionId>
88
{
9-
private string _description = null!;
109
public const int DescriptionMaxLength = 500;
1110

1211
public string Description
1312
{
14-
get => _description;
13+
get;
1514
private set
1615
{
1716
ThrowIfNullOrWhiteSpace(value, nameof(Description));
1817
ThrowIfGreaterThan(value.Length, DescriptionMaxLength, nameof(Description));
19-
_description = value;
18+
field = value;
2019
}
21-
}
20+
} = null!;
2221

2322
public MissionStatus Status { get; private set; }
2423

src/Domain/Teams/Team.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@ public class Team : AggregateRoot<TeamId>
1010
{
1111
public const int NameMaxLength = 100;
1212

13-
private string _name = null!;
1413
private readonly List<Hero> _heroes = [];
1514
private readonly List<Mission> _missions = [];
1615
private Mission? CurrentMission => _missions.FirstOrDefault(m => m.Status == MissionStatus.InProgress);
1716

1817
public string Name
1918
{
20-
get => _name;
19+
get;
2120
private set
2221
{
2322
ThrowIfNullOrWhiteSpace(value, nameof(Name));
2423
ThrowIfGreaterThan(value.Length, NameMaxLength, nameof(Name));
25-
_name = value;
24+
field = value;
2625
}
27-
}
26+
} = null!;
2827

2928
public int TotalPowerLevel { get; private set; }
3029
public TeamStatus Status { get; private set; }

0 commit comments

Comments
 (0)