Skip to content

Commit

Permalink
refactor: Use new collection initializers (#292)
Browse files Browse the repository at this point in the history
* refactor(): Use collection initializers

* version bump stylecop to 1.2.0-beta.556
  • Loading branch information
AlmarAubel committed Dec 23, 2023
1 parent e00d241 commit f04bd59
Show file tree
Hide file tree
Showing 54 changed files with 129 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static void CheckAllEndpoints()
var assembly = typeof(Startup).Assembly;
var allControllerTypes = assembly.GetTypes().Where(x => x.IsSubclassOf(typeof(ControllerBase)));

var notProtectedActionMethods = new List<string>();
List<string> notProtectedActionMethods = [];
foreach (var controllerType in allControllerTypes)
{
var controllerHasPermissionAttribute = controllerType.GetCustomAttribute<HasPermissionAttribute>();
Expand Down
2 changes: 1 addition & 1 deletion src/BuildingBlocks/Domain/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void ClearDomainEvents()
/// <param name="domainEvent">Domain event.</param>
protected void AddDomainEvent(IDomainEvent domainEvent)
{
_domainEvents ??= new List<IDomainEvent>();
_domainEvents ??= [];

this._domainEvents.Add(domainEvent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task DispatchEventsAsync()
{
var domainEvents = _domainEventsProvider.GetAllDomainEvents();

var domainEventNotifications = new List<IDomainEventNotification<IDomainEvent>>();
List<IDomainEventNotification<IDomainEvent>> domainEventNotifications = [];
foreach (var domainEvent in domainEvents)
{
Type domainEvenNotificationType = typeof(IDomainEventNotification<>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Subscribe<T>(IIntegrationEventHandler<T> handler)
}
else
{
_handlersDictionary.Add(eventType, new List<IIntegrationEventHandler> { handler });
_handlersDictionary.Add(eventType, [handler]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.507" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition="$(MSBuildProjectName.EndsWith('API'))">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void CheckMappings()
x.BaseType == typeof(InternalCommandBase)))
.ToList();

List<Type> notMappedInternalCommands = new List<Type>();
List<Type> notMappedInternalCommands = [];
foreach (var internalCommand in internalCommands)
{
_internalCommandsMap.TryGetBySecond(internalCommand, out var name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void CheckMappings()
.Where(x => x.GetInterfaces().Contains(typeof(IDomainEventNotification)))
.ToList();

List<Type> notMappedNotifications = new List<Type>();
List<Type> notMappedNotifications = [];
foreach (var domainEventNotification in domainEventNotifications)
{
_domainNotificationsMap.TryGetBySecond(domainEventNotification, out var name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void InternalCommand_Should_Have_JsonConstructorAttribute()
.Inherit(typeof(InternalCommandBase<>))
.GetTypes();

var failingTypes = new List<Type>();
List<Type> failingTypes = [];

foreach (var type in types)
{
Expand Down Expand Up @@ -155,7 +155,7 @@ public void MediatR_RequestHandler_Should_NotBe_Used_Directly()
.Should().ImplementInterface(typeof(IRequestHandler<>))
.GetTypes();

List<Type> failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var type in types)
{
bool isCommandHandler = type.GetInterfaces().Any(x =>
Expand Down Expand Up @@ -184,7 +184,7 @@ public void Command_With_Result_Should_Not_Return_Unit()
.That().ImplementInterface(commandWithResultHandlerType)
.GetTypes().ToList();

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (Type type in types)
{
Type interfaceType = type.GetInterface(commandWithResultHandlerType.Name);
Expand Down
10 changes: 5 additions & 5 deletions src/Modules/Administration/Tests/ArchTests/Domain/DomainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Entity_Which_Is_Not_Aggregate_Root_Cannot_Have_Public_Members()
BindingFlags.Instance |
BindingFlags.Static;

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var type in types)
{
var publicFields = type.GetFields(bindingFlags);
Expand Down Expand Up @@ -75,7 +75,7 @@ public void Entity_Cannot_Have_Reference_To_Other_AggregateRoot()
BindingFlags.NonPublic |
BindingFlags.Instance;

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var type in entityTypes)
{
var fields = type.GetFields(bindingFlags);
Expand Down Expand Up @@ -112,7 +112,7 @@ public void Entity_Should_Have_Parameterless_Private_Constructor()
.That()
.Inherit(typeof(Entity)).GetTypes();

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var entityType in entityTypes)
{
bool hasPrivateParameterlessConstructor = false;
Expand Down Expand Up @@ -144,7 +144,7 @@ public void Domain_Object_Should_Have_Only_Private_Constructors()
.Inherit(typeof(ValueObject))
.GetTypes();

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var domainObjectType in domainObjectTypes)
{
var constructors = domainObjectType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
Expand All @@ -167,7 +167,7 @@ public void ValueObject_Should_Have_Private_Constructor_With_Parameters_For_His_
.That()
.Inherit(typeof(ValueObject)).GetTypes();

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var entityType in valueObjects)
{
bool hasExpectedConstructor = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class TestBase

protected static void AssertAreImmutable(IEnumerable<Type> types)
{
IList<Type> failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var type in types)
{
if (type.GetFields().Any(x => !x.IsInitOnly) || type.GetProperties().Any(x => x.CanWrite))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DomainEventsTestHelper
{
public static List<IDomainEvent> GetAllDomainEvents(Entity aggregate)
{
List<IDomainEvent> domainEvents = new List<IDomainEvent>();
List<IDomainEvent> domainEvents = [];

if (aggregate.DomainEvents != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task Handle(DisableMeetingCommentingConfigurationCommand command, C
var meetingCommentingConfiguration = await _meetingCommentingConfigurationRepository.GetByMeetingIdAsync(new MeetingId(command.MeetingId));
if (meetingCommentingConfiguration == null)
{
throw new InvalidCommandException(new List<string> { "Meeting commenting configuration for disabling commenting must exist." });
throw new InvalidCommandException(["Meeting commenting configuration for disabling commenting must exist."]);
}

var meeting = await _meetingRepository.GetByIdAsync(new MeetingId(command.MeetingId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task Handle(EnableMeetingCommentingConfigurationCommand command, Ca
var meetingCommentingConfiguration = await _meetingCommentingConfigurationRepository.GetByMeetingIdAsync(new MeetingId(command.MeetingId));
if (meetingCommentingConfiguration == null)
{
throw new InvalidCommandException(new List<string> { "Meeting commenting configuration for enabling commenting must exist." });
throw new InvalidCommandException(["Meeting commenting configuration for enabling commenting must exist."]);
}

var meeting = await _meetingRepository.GetByIdAsync(new MeetingId(command.MeetingId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task<Guid> Handle(AddMeetingCommentCommand command, CancellationTok
var meeting = await _meetingRepository.GetByIdAsync(new MeetingId(command.MeetingId));
if (meeting == null)
{
throw new InvalidCommandException(new List<string> { "Meeting for adding comment must exist." });
throw new InvalidCommandException(["Meeting for adding comment must exist."]);
}

var meetingGroup = await _meetingGroupRepository.GetByIdAsync(meeting.GetMeetingGroupId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task Handle(AddMeetingCommentLikeCommand request, CancellationToken
var meetingComment = await _meetingCommentRepository.GetByIdAsync(new MeetingCommentId(request.MeetingCommentId));
if (meetingComment == null)
{
throw new InvalidCommandException(new List<string> { "To add like the comment must exist." });
throw new InvalidCommandException(["To add like the comment must exist."]);
}

var connection = _sqlConnectionFactory.GetOpenConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<Guid> Handle(AddReplyToMeetingCommentCommand command, Cancella
var meetingComment = await _meetingCommentRepository.GetByIdAsync(new MeetingCommentId(command.InReplyToCommentId));
if (meetingComment == null)
{
throw new InvalidCommandException(new List<string> { "To create reply the comment must exist." });
throw new InvalidCommandException(["To create reply the comment must exist."]);
}

var meeting = await _meetingRepository.GetByIdAsync(meetingComment.GetMeetingId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task Handle(EditMeetingCommentCommand command, CancellationToken ca
var meetingComment = await _meetingCommentRepository.GetByIdAsync(new MeetingCommentId(command.MeetingCommentId));
if (meetingComment == null)
{
throw new InvalidCommandException(new List<string> { "Meeting comment for editing must exist." });
throw new InvalidCommandException(["Meeting comment for editing must exist."]);
}

var meetingCommentingConfiguration = await _meetingCommentingConfigurationRepository.GetByMeetingIdAsync(meetingComment.GetMeetingId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task Handle(RemoveMeetingCommentCommand command, CancellationToken
var meetingComment = await _meetingCommentRepository.GetByIdAsync(new MeetingCommentId(command.MeetingCommentId));
if (meetingComment == null)
{
throw new InvalidCommandException(new List<string> { "Meeting comment for removing must exist." });
throw new InvalidCommandException(["Meeting comment for removing must exist."]);
}

var meeting = await _meetingRepository.GetByIdAsync(meetingComment.GetMeetingId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task Handle(RemoveMeetingCommentLikeCommand command, CancellationTo
var commentLike = await _meetingMemberCommentLikesRepository.GetAsync(_memberContext.MemberId, new MeetingCommentId(command.MeetingCommentId));
if (commentLike == null)
{
throw new InvalidCommandException(new List<string> { "Meeting comment like for removing must exist." });
throw new InvalidCommandException(["Meeting comment like for removing must exist."]);
}

commentLike.Remove();
Expand Down
3 changes: 1 addition & 2 deletions src/Modules/Meetings/Domain/MeetingGroups/MeetingGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ private MeetingGroup(MeetingGroupProposalId meetingGroupProposalId, string name,

this.AddDomainEvent(new MeetingGroupCreatedDomainEvent(this.Id, creatorId));

this._members = new List<MeetingGroupMember>();
this._members.Add(MeetingGroupMember.CreateNew(this.Id, this._creatorId, MeetingGroupMemberRole.Organizer));
this._members = [MeetingGroupMember.CreateNew(this.Id, this._creatorId, MeetingGroupMemberRole.Organizer)];
}

public void EditGeneralAttributes(string name, string description, MeetingGroupLocation location)
Expand Down
28 changes: 14 additions & 14 deletions src/Modules/Meetings/Domain/Meetings/Meeting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ namespace CompanyName.MyMeetings.Modules.Meetings.Domain.Meetings
{
public class Meeting : Entity, IAggregateRoot
{
public MeetingId Id { get; private set; }
private readonly MeetingGroupId _meetingGroupId;

private readonly List<MeetingAttendee> _attendees;

private readonly List<MeetingNotAttendee> _notAttendees;

private readonly List<MeetingWaitlistMember> _waitlistMembers;

private MeetingGroupId _meetingGroupId;
public MeetingId Id { get; private set; }

private string _title;

Expand All @@ -23,12 +29,6 @@ public class Meeting : Entity, IAggregateRoot

private MeetingLocation _location;

private List<MeetingAttendee> _attendees;

private List<MeetingNotAttendee> _notAttendees;

private List<MeetingWaitlistMember> _waitlistMembers;

private MeetingLimits _meetingLimits;

private Term _rsvpTerm;
Expand All @@ -51,9 +51,9 @@ public class Meeting : Entity, IAggregateRoot

private Meeting()
{
_attendees = new List<MeetingAttendee>();
_notAttendees = new List<MeetingNotAttendee>();
_waitlistMembers = new List<MeetingWaitlistMember>();
_attendees = [];
_notAttendees = [];
_waitlistMembers = [];
}

internal static Meeting CreateNew(
Expand Down Expand Up @@ -106,9 +106,9 @@ private Meeting()
_creatorId = creatorId;
_createDate = SystemClock.Now;

_attendees = new List<MeetingAttendee>();
_notAttendees = new List<MeetingNotAttendee>();
_waitlistMembers = new List<MeetingWaitlistMember>();
_attendees = [];
_notAttendees = [];
_waitlistMembers = [];

this.AddDomainEvent(new MeetingCreatedDomainEvent(this.Id));
var rsvpDate = SystemClock.Now;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void CheckMappings()
.Where(x => x.GetInterfaces().Contains(typeof(IDomainEventNotification)))
.ToList();

List<Type> notMappedNotifications = new List<Type>();
List<Type> notMappedNotifications = [];
foreach (var domainEventNotification in domainEventNotifications)
{
_domainNotificationsMap.TryGetBySecond(domainEventNotification, out var name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void InternalCommand_Should_Have_Constructor_With_JsonConstructorAttribut
.Inherit(typeof(InternalCommandBase<>))
.GetTypes();

var failingTypes = new List<Type>();
List<Type> failingTypes = [];

foreach (var type in types)
{
Expand Down Expand Up @@ -155,7 +155,7 @@ public void MediatR_RequestHandler_Should_NotBe_Used_Directly()
.Should().ImplementInterface(typeof(IRequestHandler<>))
.GetTypes();

List<Type> failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var type in types)
{
bool isCommandHandler = type.GetInterfaces().Any(x =>
Expand Down Expand Up @@ -184,7 +184,7 @@ public void Command_With_Result_Should_Not_Return_Unit()
.That().ImplementInterface(commandWithResultHandlerType)
.GetTypes().ToList();

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (Type type in types)
{
Type interfaceType = type.GetInterface(commandWithResultHandlerType.Name);
Expand Down
10 changes: 5 additions & 5 deletions src/Modules/Meetings/Tests/ArchTests/Domain/DomainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Entity_Which_Is_Not_Aggregate_Root_Cannot_Have_Public_Members()
BindingFlags.Instance |
BindingFlags.Static;

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var type in types)
{
var publicFields = type.GetFields(bindingFlags);
Expand Down Expand Up @@ -75,7 +75,7 @@ public void Entity_Cannot_Have_Reference_To_Other_AggregateRoot()
BindingFlags.NonPublic |
BindingFlags.Instance;

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var type in entityTypes)
{
var fields = type.GetFields(bindingFlags);
Expand Down Expand Up @@ -112,7 +112,7 @@ public void Entity_Should_Have_Parameterless_Private_Constructor()
.That()
.Inherit(typeof(Entity)).GetTypes();

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var entityType in entityTypes)
{
bool hasPrivateParameterlessConstructor = false;
Expand Down Expand Up @@ -144,7 +144,7 @@ public void Domain_Object_Should_Have_Only_Private_Constructors()
.Inherit(typeof(ValueObject))
.GetTypes();

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var domainObjectType in domainObjectTypes)
{
var constructors = domainObjectType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
Expand All @@ -167,7 +167,7 @@ public void ValueObject_Should_Have_Private_Constructor_With_Parameters_For_His_
.That()
.Inherit(typeof(ValueObject)).GetTypes();

var failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var entityType in valueObjects)
{
bool hasExpectedConstructor = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Modules/Meetings/Tests/ArchTests/SeedWork/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class TestBase

protected static void AssertAreImmutable(IEnumerable<Type> types)
{
IList<Type> failingTypes = new List<Type>();
List<Type> failingTypes = [];
foreach (var type in types)
{
if (type.GetFields().Any(x => !x.IsInitOnly) || type.GetProperties().Any(x => x.CanWrite))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal static class MeetingHelper
null,
null,
null,
new List<Guid> { executionContext.UserId }));
[executionContext.UserId]));

return meetingId;
}
Expand Down
Loading

0 comments on commit f04bd59

Please sign in to comment.