Skip to content

Commit 463366b

Browse files
authored
♻️ Refactor properties in domain models to use auto-implemented properties (#218)
This pull request refactors several domain model classes to simplify property backing and initialization. The main change is replacing explicit private backing fields with auto-properties using the `field` keyword in property setters, which streamlines the code and improves compatibility with Entity Framework (EF) for object persistence. ### Domain Model Refactoring * Replaced private backing fields with auto-properties for string properties in `Hero`, `Power`, `Mission`, `Team`, and `Auditable` classes. Set default values to `null!` and used the `field` keyword in property setters for cleaner initialization and EF compatibility. [[1]](diffhunk://#diff-48e8df39e867e7cb28b51266468197fee05e102ba67af34a8ef0e580076bdf24L14-R37) [[2]](diffhunk://#diff-fdb830510fd3f05e4ae74b2447761ae2dc7f11124990f4814948e3c9774c7af3L18-R38) [[3]](diffhunk://#diff-1f2bcbeb7242db733c27dec24dc70710ba549b84e584a02ced4e67b1183539beL9-R29) [[4]](diffhunk://#diff-e3a277db93c301b96c48a43e832a51d190919c13a640add5ce2da1a8d42916fbL12-R23) [[5]](diffhunk://#diff-ecb730c8c38c6c6052bf26d059c45ea9498443d3020ef28832feb7504c1d8f44L15-R28) ### Code Clean-up * Removed unnecessary backing fields from the affected classes, further simplifying the codebase and reducing redundancy. [[1]](diffhunk://#diff-48e8df39e867e7cb28b51266468197fee05e102ba67af34a8ef0e580076bdf24L14-R37) [[2]](diffhunk://#diff-fdb830510fd3f05e4ae74b2447761ae2dc7f11124990f4814948e3c9774c7af3L18-R38) [[3]](diffhunk://#diff-1f2bcbeb7242db733c27dec24dc70710ba549b84e584a02ced4e67b1183539beL9-R29) [[4]](diffhunk://#diff-e3a277db93c301b96c48a43e832a51d190919c13a640add5ce2da1a8d42916fbL12-R23) [[5]](diffhunk://#diff-ecb730c8c38c6c6052bf26d059c45ea9498443d3020ef28832feb7504c1d8f44L15-R28) ### Miscellaneous * Fixed a minor formatting issue in `Hero.cs` by removing an extraneous BOM character from the `using` statement.
1 parent 083bcd8 commit 463366b

File tree

5 files changed

+23
-33
lines changed

5 files changed

+23
-33
lines changed

src/WebApi/Common/Domain/Base/Auditable.cs

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

1313
private const string SystemUser = "System";
14-
private string _createdBy = null!;
15-
private string? _updatedBy;
1614

1715
public DateTimeOffset CreatedAt { get; private set; }
1816

1917
public string CreatedBy
2018
{
21-
get => _createdBy;
19+
get;
2220
private set
2321
{
2422
ThrowIfNullOrWhiteSpace(value, nameof(CreatedBy));
2523
ThrowIfGreaterThan(value.Length, CreatedByMaxLength, nameof(CreatedBy));
26-
_createdBy = value;
24+
field = value;
2725
}
28-
}
26+
} = null!;
2927

3028
public DateTimeOffset? UpdatedAt { get; private set; }
3129

3230
public string? UpdatedBy
3331
{
34-
get => _updatedBy;
32+
get;
3533
private set
3634
{
3735
ThrowIfNullOrWhiteSpace(value, nameof(UpdatedBy));
3836
ThrowIfGreaterThan(value.Length, UpdatedByMaxLength, nameof(UpdatedBy));
39-
_updatedBy = value;
37+
field = value;
4038
}
4139
}
4240

src/WebApi/Common/Domain/Heroes/Hero.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using SSW.VerticalSliceArchitecture.Common.Domain.Base;
1+
using SSW.VerticalSliceArchitecture.Common.Domain.Base;
22
using SSW.VerticalSliceArchitecture.Common.Domain.Teams;
33

44
namespace SSW.VerticalSliceArchitecture.Common.Domain.Heroes;
@@ -15,30 +15,27 @@ public class Hero : AggregateRoot<HeroId>
1515

1616
private readonly List<Power> _powers = [];
1717

18-
private string _name = null!;
19-
private string _alias = null!;
20-
2118
public string Name
2219
{
23-
get => _name;
20+
get;
2421
set
2522
{
2623
ThrowIfNullOrWhiteSpace(value, nameof(Name));
2724
ThrowIfGreaterThan(value.Length, NameMaxLength, nameof(Name));
28-
_name = value;
25+
field = value;
2926
}
30-
}
27+
} = null!;
3128

3229
public string Alias
3330
{
34-
get => _alias;
31+
get;
3532
set
3633
{
3734
ThrowIfNullOrWhiteSpace(value, nameof(Alias));
3835
ThrowIfGreaterThan(value.Length, AliasMaxLength, nameof(Alias));
39-
_alias = value;
36+
field = value;
4037
}
41-
}
38+
} = null!;
4239

4340
public int PowerLevel { get; private set; }
4441
public TeamId? TeamId { get; private set; }

src/WebApi/Common/Domain/Heroes/Power.cs

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

9-
private string _name = null!;
10-
private int _powerLevel;
11-
129
// Private setters needed for EF
1310
public string Name
1411
{
15-
get => _name;
12+
get;
1613
private set
1714
{
1815
ThrowIfNullOrWhiteSpace(value, nameof(Name));
1916
ThrowIfGreaterThan(value.Length, NameMaxLength, nameof(Name));
20-
_name = value;
17+
field = value;
2118
}
22-
}
19+
} = null!;
2320

2421
// Private setters needed for EF
2522
public int PowerLevel
2623
{
27-
get => _powerLevel;
24+
get;
2825
private set
2926
{
3027
ThrowIfLessThan(value, 1, nameof(PowerLevel));
3128
ThrowIfGreaterThan(value, 10, nameof(PowerLevel));
32-
_powerLevel = value;
29+
field = value;
3330
}
3431
}
3532

src/WebApi/Common/Domain/Teams/Mission.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ namespace SSW.VerticalSliceArchitecture.Common.Domain.Teams;
99

1010
public class Mission : Entity<MissionId>
1111
{
12-
private string _description = null!;
1312
public const int DescriptionMaxLength = 500;
1413

1514
public string Description
1615
{
17-
get => _description;
16+
get;
1817
private set
1918
{
2019
ThrowIfNullOrWhiteSpace(value, nameof(Description));
2120
ThrowIfGreaterThan(value.Length, DescriptionMaxLength, nameof(Description));
22-
_description = value;
21+
field = value;
2322
}
24-
}
23+
} = null!;
2524

2625
public MissionStatus Status { get; private set; }
2726

src/WebApi/Common/Domain/Teams/Team.cs

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

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

2019
public string Name
2120
{
22-
get => _name;
21+
get;
2322
private set
2423
{
2524
ThrowIfNullOrWhiteSpace(value, nameof(Name));
2625
ThrowIfGreaterThan(value.Length, NameMaxLength, nameof(Name));
27-
_name = value;
26+
field = value;
2827
}
29-
}
28+
} = null!;
3029

3130
public int TotalPowerLevel { get; private set; }
3231
public TeamStatus Status { get; private set; }

0 commit comments

Comments
 (0)