Skip to content

Commit

Permalink
Cleanup, more refactoring to collection expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
mhelleborg committed Nov 13, 2024
1 parent a382a5a commit 54267d8
Show file tree
Hide file tree
Showing 28 changed files with 62 additions and 59 deletions.
1 change: 0 additions & 1 deletion Samples/ASP.NET/Customers/CustomersController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down
7 changes: 2 additions & 5 deletions Source/Aggregates/Actors/ClusterIdentityMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ public static ClusterIdentity GetClusterIdentity<TAggregate>(TenantId tenantId,

public static (TenantId, EventSourceId) GetTenantAndEventSourceId(ClusterIdentity clusterIdentity)
{
if (clusterIdentity is null)
{
throw new ArgumentNullException(nameof(clusterIdentity));
}
var separator = clusterIdentity.Identity.IndexOf(":", StringComparison.Ordinal);
ArgumentNullException.ThrowIfNull(clusterIdentity);
var separator = clusterIdentity.Identity.IndexOf(':');

if(separator == -1)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Analyzers/ProjectionsAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static void EnsureMutationDoesNotAccessCurrentTime(SyntaxNodeAnalysisContext con
DescriptorRules.Projection.MutationUsedCurrentTime,
memberAccess.GetLocation(),
properties: properties,
new[] { memberAccess.ToFullString() }
[memberAccess.ToFullString()]
));
}
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Artifacts/Artifact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public bool CanCoexistWith(IIdentifier<ConceptAs<Guid>> identifier)
public bool CanCoexistWith(IIdentifier identifier) => identifier is IIdentifier<TId> typedIdentifier && CanCoexistWith(typedIdentifier);

/// <inheritdoc />
public bool Equals(Artifact<TId> other)
public bool Equals(Artifact<TId>? other)
{
if (ReferenceEquals(null, other))
if (other is null)
{
return false;
}
Expand All @@ -83,9 +83,9 @@ public bool Equals(Artifact<TId> other)
}

/// <inheritdoc />
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
if (obj is null)
{
return false;
}
Expand Down
1 change: 0 additions & 1 deletion Source/Common/Model/DecoratedTypeBindingsToModelAdder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Dolittle.SDK.Common.ClientSetup;
using Dolittle.SDK.Concepts;
Expand Down
2 changes: 1 addition & 1 deletion Source/Common/Model/IdentifierMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void AddBinding(IBinding binding, TValue bindingValue)
{
if (!TryGetValue(binding.Identifier.Id, out var bindings))
{
bindings = new List<IdentifierMapBinding<TValue>>();
bindings = [];
}
bindings.Add(new IdentifierMapBinding<TValue>(binding, bindingValue));
this[binding.Identifier.Id] = bindings;
Expand Down
12 changes: 6 additions & 6 deletions Source/Common/Model/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace Dolittle.SDK.Common.Model;
/// </summary>
public class ModelBuilder : IModelBuilder
{
readonly IdentifierMap<Type> _typesByIdentifier = new();
readonly IdentifierMap<object> _processorBuildersByIdentifier = new();
readonly IdentifierMap<Type> _typesByIdentifier = [];
readonly IdentifierMap<object> _processorBuildersByIdentifier = [];

/// <inheritdoc />
public void BindIdentifierToType<TIdentifier, TId>(TIdentifier identifier, Type type)
Expand Down Expand Up @@ -104,13 +104,13 @@ public IModel Build(IClientBuildResults buildResults)
var (coexistentTypes, conflictingTypes) = SplitCoexistingAndConflictingBindings(singlyBoundTypes, id);
var (coexistentProcessorBuilders, conflictingProcessorBuilders) = SplitCoexistingAndConflictingBindings(singlyBoundProcessorBuilders, id);

if (!conflictingTypes.Any() && !conflictingProcessorBuilders.Any())
if (conflictingTypes.Length == 0 && conflictingProcessorBuilders.Length == 0)
{
validBindings.AddRange(coexistentTypes.Select(_ => _.Binding).Concat(coexistentProcessorBuilders.Select(_ => _.Binding)));
continue;
}
AddFailedBuildResultsForConflictingBindings(id, conflictingTypes, conflictingProcessorBuilders, buildResults);
if (coexistentTypes.Any() || coexistentProcessorBuilders.Any())
if (coexistentTypes.Length != 0 || coexistentProcessorBuilders.Length != 0)
{
AddFailedBuildResultsForCoexistentBindings(id, coexistentTypes, coexistentProcessorBuilders, buildResults);
}
Expand All @@ -125,11 +125,11 @@ static void AddFailedBuildResultsForConflictingBindings(
IClientBuildResults buildResults)
{
var conflicts = new List<string>();
if (conflictingTypes.Any())
if (conflictingTypes.Length != 0)
{
conflicts.Add("types");
}
if (conflictingProcessorBuilders.Any())
if (conflictingProcessorBuilders.Length != 0)
{
conflicts.Add("processors");
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Diagnostics/ActivityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public static void RecordError(this Activity activity, Exception e)
{
activity
.SetStatus(ActivityStatusCode.Error)
.RecordException(e);
.AddException(e);
}
}
2 changes: 1 addition & 1 deletion Source/EventHorizon/SubscriptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Dolittle.SDK.EventHorizon;
public class SubscriptionsBuilder
{
readonly SubscriptionCallbacks _callbacks = new();
readonly IList<SubscriptionsBuilderForConsumerTenant> _builders = new List<SubscriptionsBuilderForConsumerTenant>();
readonly IList<SubscriptionsBuilderForConsumerTenant> _builders = [];

/// <summary>
/// Sets the event horizon subscriptions for the consumer tenant through the <see cref="SubscriptionsBuilderForConsumerTenant"/>.
Expand Down
4 changes: 2 additions & 2 deletions Source/EventHorizon/SubscriptionsBuilderForConsumerTenant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Dolittle.SDK.EventHorizon;
public class SubscriptionsBuilderForConsumerTenant
{
readonly SubscriptionCallbacks _callbacks = new();
readonly IList<SubscriptionBuilderForProducerMicroservice> _builders = new List<SubscriptionBuilderForProducerMicroservice>();
readonly IList<SubscriptionBuilderForProducerMicroservice> _builders = [];
readonly TenantId _consumerTenantId;

/// <summary>
Expand Down Expand Up @@ -87,4 +87,4 @@ public void BuildAndSubscribe(IEventHorizons eventHorizons, CancellationToken ca
builder.BuildAndSubscribe(eventHorizons, cancellationToken);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ public ClassEventHandlerMethod(VoidEventHandlerMethodSignature<TEventHandler> me
public async Task<Try> TryHandle(object @event, EventContext context, IServiceProvider serviceProvider)
{
using var scope = serviceProvider.CreateScope();
var eventHandler = scope.ServiceProvider.GetService<TEventHandler>();
if (eventHandler == null)
{
throw new CouldNotInstantiateEventHandler(typeof(TEventHandler));
}
var eventHandler = scope.ServiceProvider.GetService<TEventHandler>() ?? throw new CouldNotInstantiateEventHandler(typeof(TEventHandler));
return await _method(eventHandler, @event, context).TryTask().ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace Dolittle.SDK.Events.Handling.Builder.Methods;
/// </summary>
public class EventHandlerMethodsBuilder : IEventHandlerMethodsBuilder
{
readonly List<(Type, IEventHandlerMethod)> _handleMethodAndTypes = new();
readonly List<(EventType, IEventHandlerMethod)> _handleMethodAndArtifacts = new();
readonly List<(Type, IEventHandlerMethod)> _handleMethodAndTypes = [];
readonly List<(EventType, IEventHandlerMethod)> _handleMethodAndArtifacts = [];
readonly EventHandlerId _eventHandlerId;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ public async Task<Try> TryHandle(object @event, EventContext context, IServicePr
}

using var scope = serviceProvider.CreateScope();
var eventHandler = scope.ServiceProvider.GetService<TEventHandler>();
if (eventHandler == null)
{
throw new CouldNotInstantiateEventHandler(typeof(TEventHandler));
}

var eventHandler = scope.ServiceProvider.GetService<TEventHandler>() ?? throw new CouldNotInstantiateEventHandler(typeof(TEventHandler));
return await _method(eventHandler, typedEvent, context).TryTask().ConfigureAwait(false);
}
}
20 changes: 19 additions & 1 deletion Source/Events.Handling/IEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface IEventHandler
ScopeId ScopeId { get; }

/// <summary>
/// Gets a value indicating whether or not the event handler is partitioned.
/// Gets a value indicating whether the event handler is partitioned.
/// </summary>
bool Partitioned { get; }

Expand All @@ -44,9 +44,27 @@ public interface IEventHandler
/// </summary>
bool HasAlias { get; }

/// <summary>
/// Sets the concurrency for the event handler. Defaults to 1, meaning that the event handler is single threaded.
/// If > 1, the handler can process multiple events concurrently, so long as they are on separte partitions (event source)
/// </summary>
int Concurrency { get; }

/// <summary>
/// Should it start from the end of the current stream or from the beginning.
/// </summary>
ProcessFrom ResetTo { get; }

/// <summary>
/// The handler can be configured to start from a specific point in time.
/// Events that were committed before this point in time will not be processed.
/// </summary>
DateTimeOffset? StartFrom { get; }

/// <summary>
/// The handler can be configured to stop at a specific point in time.
/// Events that were committed after this point in time will not be processed.
/// </summary>
DateTimeOffset? StopAt { get; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Dolittle.SDK.Events.Store.Builders;
public class UncommittedAggregateEventsBuilder
{
readonly EventSourceId _eventSourceId;
readonly IList<EventBuilder> _builders = new List<EventBuilder>();
readonly IList<EventBuilder> _builders = [];
readonly AggregateRootId _aggregateRootId;
readonly AggregateRootVersion _expectedVersion;

Expand Down Expand Up @@ -66,4 +66,4 @@ EventBuilder CreateEvent(object content, bool isPublic)
_builders.Add(builder);
return builder;
}
}
}
4 changes: 2 additions & 2 deletions Source/Events/Store/Builders/UncommittedEventsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Dolittle.SDK.Events.Store.Builders;
/// </summary>
public class UncommittedEventsBuilder
{
readonly IList<UncommittedEventBuilder> _builders = new List<UncommittedEventBuilder>();
readonly IList<UncommittedEventBuilder> _builders = [];

/// <summary>
/// Build an event.
Expand Down Expand Up @@ -46,4 +46,4 @@ UncommittedEventBuilder CreateBuilder(object @event, bool isPublic)
_builders.Add(builder);
return builder;
}
}
}
2 changes: 1 addition & 1 deletion Source/Events/Store/CommittedAggregateEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public CommittedAggregateEvents(EventSourceId eventSource, AggregateRootId aggre
ThrowIfEventLogVersionIsOutOfOrder(@event, events[i - 1]);
}

_events = ImmutableList<CommittedAggregateEvent>.Empty.AddRange(events);
_events = [.. events];
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Source/Events/Store/CommittedEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public CommittedEvents(IReadOnlyList<CommittedEvent> events)
if (i > 0) ThrowIfEventLogVersionIsOutOfOrder(@event, events[i - 1]);
}

_events = ImmutableList<CommittedEvent>.Empty.AddRange(events);
_events = [.. events];
}

/// <summary>
Expand Down Expand Up @@ -56,4 +56,4 @@ void ThrowIfEventLogVersionIsOutOfOrder(CommittedEvent @event, CommittedEvent pr
{
if (@event.EventLogSequenceNumber <= previousEvent.EventLogSequenceNumber) throw new EventLogSequenceNumberIsOutOfOrder(@event.EventLogSequenceNumber, previousEvent.EventLogSequenceNumber);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public CouldNotDeserializeEventContent(
EventLogSequenceNumber sequenceNumber,
string json,
Exception innerException,
Type type = default)
Type? type = default)
: base(CreateExceptionMessage(eventType, sequenceNumber, json, type), innerException)
{
}

static string CreateExceptionMessage(EventType eventType, EventLogSequenceNumber sequenceNumber, string json, Type type)
static string CreateExceptionMessage(EventType eventType, EventLogSequenceNumber sequenceNumber, string json, Type? type)
=> type == default
? $"Could not deserialize event with sequence number {sequenceNumber} and event type {eventType}. Content is {json}"
: $"Could not deserialize event with sequence number {sequenceNumber} and event type {eventType} to {type}. Content is {json}";
Expand Down
4 changes: 2 additions & 2 deletions Source/Events/Store/Internal/EventsForAggregateFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ static async Task<CommittedAggregateEvents> AggregateBatches(EventSourceId event
IAsyncEnumerable<CommittedAggregateEvents> batches)
{
var fetchedEvents =
new CommittedAggregateEvents(eventSourceId, aggregateRootId, AggregateRootVersion.Initial, ImmutableList<CommittedAggregateEvent>.Empty);
new CommittedAggregateEvents(eventSourceId, aggregateRootId, AggregateRootVersion.Initial, []);
await foreach (var batch in batches)
{
fetchedEvents = new CommittedAggregateEvents(eventSourceId, aggregateRootId, batch.AggregateRootVersion, fetchedEvents.Concat(batch).ToList());
fetchedEvents = new CommittedAggregateEvents(eventSourceId, aggregateRootId, batch.AggregateRootVersion, [.. fetchedEvents, .. batch]);
}

return fetchedEvents;
Expand Down
4 changes: 2 additions & 2 deletions Source/Events/Store/UncommittedAggregateEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Dolittle.SDK.Events.Store;
/// </summary>
public class UncommittedAggregateEvents : IList<UncommittedAggregateEvent>
{
readonly List<UncommittedAggregateEvent> _events = new();
readonly List<UncommittedAggregateEvent> _events = [];

/// <summary>
/// Initializes a new instance of the <see cref="UncommittedAggregateEvents"/> class.
Expand Down Expand Up @@ -128,4 +128,4 @@ void ThrowIfEventIsNull(UncommittedAggregateEvent @event)
{
if (@event == null) throw new EventCannotBeNull();
}
}
}
4 changes: 2 additions & 2 deletions Source/Events/Store/UncommittedEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Dolittle.SDK.Events.Store;
/// </summary>
public class UncommittedEvents : IList<UncommittedEvent>
{
readonly List<UncommittedEvent> _events = new();
readonly List<UncommittedEvent> _events = [];

/// <summary>
/// Gets a value indicating whether or not there are any events in the committed sequence.
Expand Down Expand Up @@ -81,4 +81,4 @@ void ThrowIfEventIsNull(UncommittedEvent @event)
{
if (@event == null) throw new EventCannotBeNull();
}
}
}
4 changes: 2 additions & 2 deletions Source/Projections/Actors/ProjectionActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void OnSubscriptionRequest(SubscriptionRequest request, IContext context)
}

RespondWithCurrentValue(context);
_subscribers ??= new HashSet<PID>();
_subscribers ??= [];
_subscribers.Add(request.Subscriber);
context.CancelReceiveTimeout(); // Keep the actor alive as long as there are subscribers
}
Expand Down Expand Up @@ -190,7 +190,7 @@ void OnWaitForUpdate(ulong requestedOffset, IContext context)
return;
}

_waitingForUpdate ??= new Dictionary<ulong, TaskCompletionSource>();
_waitingForUpdate ??= [];

if (!_waitingForUpdate.TryGetValue(requestedOffset, out var tcs))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Dolittle.SDK.Projections.Builder;
public class ProjectionBuilderForReadModel<TReadModel> : IProjectionBuilderForReadModel<TReadModel>, ICanTryBuildProjection
where TReadModel : ReadModel, new()
{
readonly IList<IProjectionMethod<TReadModel>> _methods = new List<IProjectionMethod<TReadModel>>();
readonly IList<IProjectionMethod<TReadModel>> _methods = [];
readonly ProjectionId _projectionId;
ProjectionAlias _alias;
ScopeId _scopeId;
Expand Down
2 changes: 1 addition & 1 deletion Source/Projections/Internal/ProjectionsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected override async Task<EventHandlerResponse> Process(HandleEventRequest r
catch (Exception e)
{
_logger.ErrorProcessingProjectionEvent(e, committedEvent);
activity?.RecordException(e);
activity?.AddException(e);
throw;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Security/Claims.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Claims : IEnumerable<Claim>, IEquatable<Claims>
/// <summary>
/// Gets the empty representation of <see cref="Claims"/>.
/// </summary>
public static readonly Claims Empty = new(Array.Empty<Claim>());
public static readonly Claims Empty = new([]);

readonly List<Claim> _claims = [];

Expand Down
2 changes: 1 addition & 1 deletion Source/Services/ProcessingCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Dolittle.SDK.Services;
/// </summary>
public class ProcessingCoordinator : ICoordinateProcessing
{
readonly List<Task> _processors = new();
readonly List<Task> _processors = [];

/// <inheritdoc/>
public Task Completion => Task.WhenAll(_processors);
Expand Down
1 change: 0 additions & 1 deletion Tests/ProjectionsTests/PropertyKeyedProjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Dolittle.SDK.Events;
using Dolittle.SDK.Testing.Projections;
using FluentAssertions;
using MongoDB.Bson.Serialization.Attributes;
using Xunit;

namespace Dolittle.SDK.Projections;
Expand Down

0 comments on commit 54267d8

Please sign in to comment.