From 2027b4e4b62c45ff274d9437113820a53dfbcd88 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Thu, 18 Sep 2025 18:47:22 +0000 Subject: [PATCH 1/2] refactor: drop redundant types from object instantiations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR refactors instantiation expressions to use C# 9 target-typed new, removing redundant type specifications on the right-hand side of declarations. These changes streamline the codebase, reduce verbosity, and improve readability. - Type can be dropped from the declaration's RHS when explicitly mentioned in the LHS: DeepSource highlighted numerous cases where the explicit type in expressions like `new HttpClient()` or `new List()` was unnecessary because the variable’s type was already known. All such instantiations have been replaced with the concise `new()` syntax, leveraging compiler type inference and ensuring consistent, modern C# style across the project. > This Autofix was generated by AI. Please review the change before merging. --- CallbackHander.Testing/TestData.cs | 2 +- .../Mediator/MediatorTests.cs | 10 +++++----- .../CallbackHandlerRequestHandlerTests.cs | 14 ++++++-------- .../CallbackMessageAggregateTests.cs | 2 +- .../Common/DockerHelper.cs | 2 +- .../Shared/SharedSteps.cs | 12 ++++++------ CallbackHandler.Tests/BootstrapperTests.cs | 8 ++++---- CallbackHandler/Bootstrapper/MiddlewareRegistry.cs | 4 ++-- CallbackHandler/Program.cs | 2 +- 9 files changed, 27 insertions(+), 29 deletions(-) diff --git a/CallbackHander.Testing/TestData.cs b/CallbackHander.Testing/TestData.cs index f5f7dee..a0e1ff1 100644 --- a/CallbackHander.Testing/TestData.cs +++ b/CallbackHander.Testing/TestData.cs @@ -41,7 +41,7 @@ public static CallbackMessageAggregate EmptyCallbackMessageAggregate() public static CallbackMessageAggregate RecordedCallbackMessageAggregate() { - CallbackMessageAggregate aggregate = new CallbackMessageAggregate(); + CallbackMessageAggregate aggregate = new(); aggregate.RecordCallback(TestData.CallbackId, TestData.TypeString, (MessageFormat)TestData.MessageFormat, TestData.CallbackMessage, TestData.Reference, TestData.Destinations, EstateReference, MerchantReference); diff --git a/CallbackHandler.BusinessLogic.Tests/Mediator/MediatorTests.cs b/CallbackHandler.BusinessLogic.Tests/Mediator/MediatorTests.cs index 51c1fa6..aef1d74 100644 --- a/CallbackHandler.BusinessLogic.Tests/Mediator/MediatorTests.cs +++ b/CallbackHandler.BusinessLogic.Tests/Mediator/MediatorTests.cs @@ -30,20 +30,20 @@ public MediatorTests() [Fact] public async Task Mediator_Send_RequestHandled() { - Mock hostingEnvironment = new Mock(); + Mock hostingEnvironment = new(); hostingEnvironment.Setup(he => he.EnvironmentName).Returns("Development"); hostingEnvironment.Setup(he => he.ContentRootPath).Returns("/home"); hostingEnvironment.Setup(he => he.ApplicationName).Returns("Test Application"); - ServiceRegistry services = new ServiceRegistry(); - Startup s = new Startup(hostingEnvironment.Object); + ServiceRegistry services = new(); + Startup s = new(hostingEnvironment.Object); Startup.Configuration = this.SetupMemoryConfiguration(); this.AddTestRegistrations(services, hostingEnvironment.Object); s.ConfigureContainer(services); Startup.Container.AssertConfigurationIsValid(AssertMode.Full); - List errors = new List(); + List errors = new(); IMediator mediator = Startup.Container.GetService(); foreach (IBaseRequest baseRequest in this.Requests) { @@ -90,7 +90,7 @@ private void AddTestRegistrations(ServiceRegistry services, IWebHostEnvironment hostingEnvironment) { services.AddLogging(); - DiagnosticListener diagnosticSource = new DiagnosticListener(hostingEnvironment.ApplicationName); + DiagnosticListener diagnosticSource = new(hostingEnvironment.ApplicationName); services.AddSingleton(diagnosticSource); services.AddSingleton(diagnosticSource); services.AddSingleton(hostingEnvironment); diff --git a/CallbackHandler.BusinessLogic.Tests/RequestHandler/CallbackHandlerRequestHandlerTests.cs b/CallbackHandler.BusinessLogic.Tests/RequestHandler/CallbackHandlerRequestHandlerTests.cs index fd24131..349c240 100644 --- a/CallbackHandler.BusinessLogic.Tests/RequestHandler/CallbackHandlerRequestHandlerTests.cs +++ b/CallbackHandler.BusinessLogic.Tests/RequestHandler/CallbackHandlerRequestHandlerTests.cs @@ -23,8 +23,7 @@ public class CallbackHandlerRequestHandlerTests [Fact] public void CallbackHandlerRequestHandlerTests_RecordCallbackRequest_IsHandled() { - Mock domainService = - new Mock(); + Mock domainService = new(); domainService.Setup(a => a.RecordCallback(It.IsAny(), It.IsAny(), It.IsAny(), @@ -32,10 +31,9 @@ public void CallbackHandlerRequestHandlerTests_RecordCallbackRequest_IsHandled() It.IsAny(), It.IsAny(), It.IsAny())); - Mock> aggregateRepository = - new Mock>(); + Mock> aggregateRepository = new(); - CallbackHandlerRequestHandler handler = new CallbackHandlerRequestHandler(domainService.Object, aggregateRepository.Object); + CallbackHandlerRequestHandler handler = new(domainService.Object, aggregateRepository.Object); CallbackCommands.RecordCallbackRequest request = TestData.RecordCallbackRequest; @@ -49,7 +47,7 @@ public void CallbackHandlerRequestHandlerTests_RecordCallbackRequest_IsHandled() public void CallbackHandlerRequestHandlerTests_GetCallbackQuery_IsHandled() { Mock domainService = - new Mock(); + new(); domainService.Setup(a => a.RecordCallback(It.IsAny(), It.IsAny(), It.IsAny(), @@ -58,12 +56,12 @@ public void CallbackHandlerRequestHandlerTests_GetCallbackQuery_IsHandled() It.IsAny(), It.IsAny())); Mock> aggregateRepository = - new Mock>(); + new(); aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.RecordedCallbackMessageAggregate()); - CallbackHandlerRequestHandler handler = new CallbackHandlerRequestHandler(domainService.Object, aggregateRepository.Object); + CallbackHandlerRequestHandler handler = new(domainService.Object, aggregateRepository.Object); CallbackQueries.GetCallbackQuery query = TestData.GetCallbackQuery; diff --git a/CallbackHandler.CallbackMessageAggregate.Tests/CallbackMessageAggregateTests.cs b/CallbackHandler.CallbackMessageAggregate.Tests/CallbackMessageAggregateTests.cs index 1e55d5f..67ef6e3 100644 --- a/CallbackHandler.CallbackMessageAggregate.Tests/CallbackMessageAggregateTests.cs +++ b/CallbackHandler.CallbackMessageAggregate.Tests/CallbackMessageAggregateTests.cs @@ -20,7 +20,7 @@ public class CallbackMessageAggregateTests [Fact] public void CallbackMessageAggregate_RecordCallback_CallbackIsRecorded() { - CallbackMessageAggregate aggregate = new CallbackMessageAggregate(); + CallbackMessageAggregate aggregate = new(); Result result = aggregate.RecordCallback(TestData.CallbackId, TestData.TypeString, MessageFormat.JSON, TestData.CallbackMessage, TestData.Reference, TestData.Destinations, TestData.EstateReference, TestData.MerchantReference); diff --git a/CallbackHandler.IntegrationTests/Common/DockerHelper.cs b/CallbackHandler.IntegrationTests/Common/DockerHelper.cs index a20221b..b6e6d22 100644 --- a/CallbackHandler.IntegrationTests/Common/DockerHelper.cs +++ b/CallbackHandler.IntegrationTests/Common/DockerHelper.cs @@ -49,7 +49,7 @@ public async Task StartSystem() { // Initialise a logger String scenarioName = this.ScenarioContext.ScenarioInfo.Title.Replace(" ", ""); - NlogLogger logger = new NlogLogger(); + NlogLogger logger = new(); logger.Initialise(LogManager.GetLogger(scenarioName), scenarioName); LogManager.AddHiddenAssembly(typeof(NlogLogger).Assembly); diff --git a/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs b/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs index 9fc1645..c123a03 100644 --- a/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs +++ b/CallbackHandler.IntegrationTests/Shared/SharedSteps.cs @@ -40,12 +40,12 @@ public async Task GivenIHaveTheFollowingBankDepositCallbacks(DataTable table) [When(@"I send the requests to the callback handler for deposits")] public async Task WhenISendTheRequestsToTheCallbackHandlerForDeposits() { - using HttpClient client = new HttpClient(); + using HttpClient client = new(); foreach (var testingContextDeposit in this.TestingContext.Deposits) { String requestUri = $"http://localhost:{this.TestingContext.DockerHelper.GetCallbackHandlerPort()}/api/callbacks"; - HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Post, requestUri); + HttpRequestMessage msg = new(HttpMethod.Post, requestUri); var payload = JsonConvert.SerializeObject(testingContextDeposit); msg.Content = new StringContent(payload, Encoding.UTF8, @@ -63,12 +63,12 @@ public async Task WhenISendTheRequestsToTheCallbackHandlerForDeposits() [Then("the deposit records are recorded")] public async Task ThenTheDepositRecordsAreRecorded() { - using HttpClient client = new HttpClient(); + using HttpClient client = new(); foreach (var sentCallback in this.TestingContext.SentCallbacks) { String requestUri = $"http://localhost:{this.TestingContext.DockerHelper.GetCallbackHandlerPort()}/api/callbacks/{sentCallback.Key}"; - HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, requestUri); + HttpRequestMessage msg = new(HttpMethod.Get, requestUri); var response = await client.SendAsync(msg); response.StatusCode.ShouldBe(HttpStatusCode.OK); @@ -90,10 +90,10 @@ public static class ReqnrollExtensions { public static List ToDepositRequests(this DataTableRows tableRows) { - List requests = new List(); + List requests = new(); foreach (DataTableRow tableRow in tableRows) { - Deposit depositCallback = new Deposit + Deposit depositCallback = new() { AccountNumber = ReqnrollTableHelper.GetStringRowValue(tableRow, "AccountNumber"), Amount = ReqnrollTableHelper.GetDecimalValue(tableRow, "Amount"), diff --git a/CallbackHandler.Tests/BootstrapperTests.cs b/CallbackHandler.Tests/BootstrapperTests.cs index 3da2ea2..aa922f9 100644 --- a/CallbackHandler.Tests/BootstrapperTests.cs +++ b/CallbackHandler.Tests/BootstrapperTests.cs @@ -18,13 +18,13 @@ public class BootstrapperTests [Fact] public void VerifyBootstrapperIsValid() { - Mock hostingEnvironment = new Mock(); + Mock hostingEnvironment = new(); hostingEnvironment.Setup(he => he.EnvironmentName).Returns("Development"); hostingEnvironment.Setup(he => he.ContentRootPath).Returns("/home"); hostingEnvironment.Setup(he => he.ApplicationName).Returns("Test Application"); - ServiceRegistry services = new ServiceRegistry(); - Startup s = new Startup(hostingEnvironment.Object); + ServiceRegistry services = new(); + Startup s = new(hostingEnvironment.Object); Startup.Configuration = this.SetupMemoryConfiguration(); this.AddTestRegistrations(services, hostingEnvironment.Object); @@ -58,7 +58,7 @@ private void AddTestRegistrations(ServiceRegistry services, IWebHostEnvironment hostingEnvironment) { services.AddLogging(); - DiagnosticListener diagnosticSource = new DiagnosticListener(hostingEnvironment.ApplicationName); + DiagnosticListener diagnosticSource = new(hostingEnvironment.ApplicationName); services.AddSingleton(diagnosticSource); services.AddSingleton(diagnosticSource); services.AddSingleton(hostingEnvironment); diff --git a/CallbackHandler/Bootstrapper/MiddlewareRegistry.cs b/CallbackHandler/Bootstrapper/MiddlewareRegistry.cs index 050ffd4..d5c73d6 100644 --- a/CallbackHandler/Bootstrapper/MiddlewareRegistry.cs +++ b/CallbackHandler/Bootstrapper/MiddlewareRegistry.cs @@ -53,7 +53,7 @@ public MiddlewareRegistry() c.ExampleFilters(); //Locate the XML files being generated by ASP.NET... - DirectoryInfo directory = new DirectoryInfo(AppContext.BaseDirectory); + DirectoryInfo directory = new(AppContext.BaseDirectory); FileInfo[] xmlFiles = directory.GetFiles("*.xml"); //... and tell Swagger to use those XML comments. @@ -82,7 +82,7 @@ public MiddlewareRegistry() LogLevel middlewareLogLevel = ConfigurationReader.GetValueOrDefault("MiddlewareLogging", "MiddlewareLogLevel", LogLevel.Warning); RequestResponseMiddlewareLoggingConfig config = - new RequestResponseMiddlewareLoggingConfig(middlewareLogLevel, logRequests, logResponses); + new(middlewareLogLevel, logRequests, logResponses); this.AddSingleton(config); } diff --git a/CallbackHandler/Program.cs b/CallbackHandler/Program.cs index 763aae1..b86a250 100644 --- a/CallbackHandler/Program.cs +++ b/CallbackHandler/Program.cs @@ -31,7 +31,7 @@ public static void Main(string[] args) public static IHostBuilder CreateHostBuilder(string[] args) { //At this stage, we only need our hosting file for ip and ports - FileInfo fi = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location); + FileInfo fi = new(System.Reflection.Assembly.GetExecutingAssembly().Location); IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(fi.Directory.FullName) .AddJsonFile("hosting.json", optional: true) From 1ae638278e383592d61801cec1e48491e177af12 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Thu, 18 Sep 2025 19:13:06 +0000 Subject: [PATCH 2/2] refactor: use typeof(Program).Assembly to get currently executing assembly This PR refactors how we obtain the currently executing assembly path, replacing the use of Assembly.GetExecutingAssembly() with typeof(Program).Assembly for improved clarity and performance. - Consider using `typeof(T).Assembly` to get currently executing assembly: The original code used Assembly.GetExecutingAssembly().Location, which relies on reflection and can be less direct. The change replaces this with `typeof(Program).Assembly.Location`, leveraging compile-time type metadata for clearer intent and potential performance gains. > This Autofix was generated by AI. Please review the change before merging. --- CallbackHandler/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CallbackHandler/Program.cs b/CallbackHandler/Program.cs index 250c723..8f6a9fc 100644 --- a/CallbackHandler/Program.cs +++ b/CallbackHandler/Program.cs @@ -32,7 +32,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) { //At this stage, we only need our hosting file for ip and ports - FileInfo fi = new(System.Reflection.Assembly.GetExecutingAssembly().Location); + FileInfo fi = new(typeof(Program).Assembly.Location); IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(fi.Directory.FullName) .AddJsonFile("hosting.json", optional: true)