From 42a2b964fc584b0c374ab462030cafd4ec145812 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Thu, 18 Sep 2025 20:36:56 +0000 Subject: [PATCH] refactor: remove redundant type from object creation expressions This PR refactors multiple object instantiation sites to leverage C# target-typed `new` expressions, removing redundant type names from the right-hand side when the type is already declared on the left. All occurrences of `new Type(...)` or `new Type()` have been updated to `new(...)` or `new()` respectively, improving code readability and reducing verbosity. - Type can be dropped from the declaration's RHS when explicitly mentioned in the LHS: In the original code, various constructors repeated the type on the right-hand side even though the variable or property type was clearly specified on the left. This change replaces those redundant instantiations with target-typed `new` expressions (`new(...)` or `new()`), streamlining the code and adhering to modern C# best practices. > This Autofix was generated by AI. Please review the change before merging. --- Driver/Program.cs | 6 +++--- Shared.EventStore/Aggregate/Aggregate.cs | 2 +- Shared.EventStore/Aggregate/AggregateRepository.cs | 2 +- Shared.EventStore/Aggregate/DomainEventFactory.cs | 2 +- Shared.EventStore/Aggregate/EventDataFactory.cs | 2 +- Shared.EventStore/Aggregate/TypeMapConvertor.cs | 4 ++-- Shared.EventStore/EventStore/EventStoreContext.cs | 2 +- Shared.EventStoreContext.Tests/EventStoreContextTests.cs | 6 +++--- Shared/EntityFramework/DbContextResolver.cs | 2 +- 9 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Driver/Program.cs b/Driver/Program.cs index 4ba783a8..198e13d9 100644 --- a/Driver/Program.cs +++ b/Driver/Program.cs @@ -49,9 +49,9 @@ private static async Task Main(String[] args) { private static async Task QueryTest(){ String query = "fromStream('$et-EstateCreatedEvent')\r\n .when({\r\n $init: function (s, e)\r\n {\r\n return {\r\n estates:[]\r\n };\r\n },\r\n \"EstateCreatedEvent\": function(s,e){\r\n s.estates.push(e.data.estateName);\r\n }\r\n });"; - EventStoreClient client = new EventStoreClient(Program.ConfigureEventStoreSettings()); - EventStoreProjectionManagementClient projection = new EventStoreProjectionManagementClient(Program.ConfigureEventStoreSettings()); - EventStoreContext context = new EventStoreContext(client, projection); + EventStoreClient client = new(Program.ConfigureEventStoreSettings()); + EventStoreProjectionManagementClient projection = new(Program.ConfigureEventStoreSettings()); + EventStoreContext context = new(client, projection); var result = await context.RunTransientQuery(query, CancellationToken.None); Console.WriteLine(result); diff --git a/Shared.EventStore/Aggregate/Aggregate.cs b/Shared.EventStore/Aggregate/Aggregate.cs index b3d93df7..e1e4ec4d 100644 --- a/Shared.EventStore/Aggregate/Aggregate.cs +++ b/Shared.EventStore/Aggregate/Aggregate.cs @@ -83,7 +83,7 @@ public void ApplyAndAppend(IDomainEvent domainEvent) { this.PendingEvents.Add(domainEvent); } catch(Exception e) { - Exception ex = new Exception($"Failed to apply event {domainEvent.EventType} to Aggregate {this.GetType().Name}", e); + Exception ex = new($"Failed to apply event {domainEvent.EventType} to Aggregate {this.GetType().Name}", e); throw ex; } diff --git a/Shared.EventStore/Aggregate/AggregateRepository.cs b/Shared.EventStore/Aggregate/AggregateRepository.cs index f1ab4100..9d1a8b80 100644 --- a/Shared.EventStore/Aggregate/AggregateRepository.cs +++ b/Shared.EventStore/Aggregate/AggregateRepository.cs @@ -123,7 +123,7 @@ private Result ProcessEvents(TAggregate aggregate, } catch (Exception e) { Exception ex = - new Exception( + new( $"Failed to apply domain event {@event.EventType} to Aggregate {aggregate.GetType()} ", e); throw ex; diff --git a/Shared.EventStore/Aggregate/DomainEventFactory.cs b/Shared.EventStore/Aggregate/DomainEventFactory.cs index 7811cb90..97253576 100644 --- a/Shared.EventStore/Aggregate/DomainEventFactory.cs +++ b/Shared.EventStore/Aggregate/DomainEventFactory.cs @@ -23,7 +23,7 @@ public class DomainEventFactory : IDomainEventFactory /// public DomainEventFactory() { - JsonIgnoreAttributeIgnorerContractResolver jsonIgnoreAttributeIgnorerContractResolver = new JsonIgnoreAttributeIgnorerContractResolver(); + JsonIgnoreAttributeIgnorerContractResolver jsonIgnoreAttributeIgnorerContractResolver = new(); JsonConvert.DefaultSettings = () => { diff --git a/Shared.EventStore/Aggregate/EventDataFactory.cs b/Shared.EventStore/Aggregate/EventDataFactory.cs index 59b036d4..178c4d65 100644 --- a/Shared.EventStore/Aggregate/EventDataFactory.cs +++ b/Shared.EventStore/Aggregate/EventDataFactory.cs @@ -59,7 +59,7 @@ public EventData CreateEventData(IDomainEvent domainEvent) Byte[] data = Encoding.Default.GetBytes(JsonConvert.SerializeObject(domainEvent)); - EventData eventData = new EventData(Uuid.FromGuid(domainEvent.EventId), domainEvent.EventType, data); + EventData eventData = new(Uuid.FromGuid(domainEvent.EventId), domainEvent.EventType, data); return eventData; } diff --git a/Shared.EventStore/Aggregate/TypeMapConvertor.cs b/Shared.EventStore/Aggregate/TypeMapConvertor.cs index 21953999..054ef11a 100644 --- a/Shared.EventStore/Aggregate/TypeMapConvertor.cs +++ b/Shared.EventStore/Aggregate/TypeMapConvertor.cs @@ -17,7 +17,7 @@ public static class TypeMapConvertor /// /// The domain event record factory /// - private static readonly DomainEventFactory StandardDomainEventFactory = new DomainEventFactory(); + private static readonly DomainEventFactory StandardDomainEventFactory = new(); private static IDomainEventFactory OverrideDomainEventFactory = null; @@ -62,7 +62,7 @@ public static IDomainEvent Convertor(Guid aggregateId, ResolvedEvent @event) /// public static EventData Convertor(IDomainEvent @event) { - EventDataFactory eventDataFactory = new EventDataFactory(); + EventDataFactory eventDataFactory = new(); return eventDataFactory.CreateEventData(@event); } diff --git a/Shared.EventStore/EventStore/EventStoreContext.cs b/Shared.EventStore/EventStore/EventStoreContext.cs index 709bdcd0..ed17ac6c 100644 --- a/Shared.EventStore/EventStore/EventStoreContext.cs +++ b/Shared.EventStore/EventStore/EventStoreContext.cs @@ -268,7 +268,7 @@ public async Task> RunTransientQuery(String query, CancellationTo catch (RpcException rex) { this.LogError(rex); - Exception ex = new Exception(ProjectionRunningStatus.Faulted.ToString(), rex); + Exception ex = new(ProjectionRunningStatus.Faulted.ToString(), rex); return Result.Failure(ex.GetExceptionMessages()); } finally diff --git a/Shared.EventStoreContext.Tests/EventStoreContextTests.cs b/Shared.EventStoreContext.Tests/EventStoreContextTests.cs index 9cc49831..1d3c9026 100644 --- a/Shared.EventStoreContext.Tests/EventStoreContextTests.cs +++ b/Shared.EventStoreContext.Tests/EventStoreContextTests.cs @@ -20,7 +20,7 @@ public class EventStoreContextTests : IDisposable{ public EventStoreContextTests() { - NlogLogger logger = new NlogLogger(); + NlogLogger logger = new(); LogManager.Setup(b => { b.SetupLogFactory(setup => setup.AddCallSiteHiddenAssembly(typeof(NlogLogger).Assembly)); b.SetupLogFactory(setup => setup.AddCallSiteHiddenAssembly(typeof(Shared.Logger.Logger).Assembly)); @@ -30,7 +30,7 @@ public EventStoreContextTests() logger.Initialise(LogManager.GetLogger("Reqnroll"), "Reqnroll"); - this.EventStoreDockerHelper = new EventStoreDockerHelper { Logger = logger }; + this.EventStoreDockerHelper = new() { Logger = logger }; } #endregion @@ -66,7 +66,7 @@ public async Task TearDown(){ [TestCase("COMPLETED/STOPPED/WRITING RESULTS", ProjectionRunningStatus.Completed)] [TestCase("Unknown", ProjectionRunningStatus.Unknown)] public void EventStoreContext_GetStatusFrom_CorrectValueReturned(String status, ProjectionRunningStatus expected){ - ProjectionDetails projectionDetails = new ProjectionDetails(0, + ProjectionDetails projectionDetails = new(0, 0, 0, String.Empty, diff --git a/Shared/EntityFramework/DbContextResolver.cs b/Shared/EntityFramework/DbContextResolver.cs index 8590fb6c..4655e423 100644 --- a/Shared/EntityFramework/DbContextResolver.cs +++ b/Shared/EntityFramework/DbContextResolver.cs @@ -41,7 +41,7 @@ public ResolvedDbContext Resolve(String connectionStringKey, String da // Update the connection string with the identifier if needed if (!String.IsNullOrWhiteSpace(databaseNameSuffix)) { - SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString); + SqlConnectionStringBuilder builder = new(connectionString); builder.InitialCatalog = $"{builder.InitialCatalog}-{databaseNameSuffix}"; connectionString = builder.ConnectionString;