Skip to content

Refactor: Extract LoadProductsWithFees to reduce GetContract complexity#461

Merged
StuartFerguson merged 2 commits intomainfrom
copilot/refactor-get-contract-method
Feb 27, 2026
Merged

Refactor: Extract LoadProductsWithFees to reduce GetContract complexity#461
StuartFerguson merged 2 commits intomainfrom
copilot/refactor-get-contract-method

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 27, 2026

ReportingManager::GetContract exceeded Codacy's 50-line method complexity limit at ~81 lines, mixing contract base loading, product/fee querying, and model assembly in a single method.

Changes

  • GetContract reduced from ~81 → ~33 lines by delegating Steps 2 & 3 to a new private helper
  • New LoadProductsWithFees(context, contractId, cancellationToken) encapsulates:
    • Loading ContractProducts for the given contract
    • Loading ContractProductTransactionFees for those products
    • Assembling List<ContractProduct> with fees populated
  • Performance: replaced O(n×m) fees.Where(f => f.ContractProductId == p.ContractProductId) with ToLookup() for O(1) fee lookups per product
// Before: monolithic 81-line method
public async Task<Result<Contract>> GetContract(...) {
    // Step 1 ... Step 2 ... Step 3 ... assembly all inline
}

// After: Step 1 + delegation
public async Task<Result<Contract>> GetContract(...) {
    // Step 1: load base contract with operator name (left-join)
    var productsResult = await this.LoadProductsWithFees(context, baseContract.ContractId, cancellationToken);
    return Result.Success(new Contract { ..., Products = productsResult.Data });
}

private async Task<Result<List<ContractProduct>>> LoadProductsWithFees(...) { ... }

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • f.feedz.io
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp (dns block)
    • Triggering command: /usr/bin/dotnet dotnet restore --no-dependencies /home/REDACTED/work/EstateReportingAPI/EstateReportingAPI/EstateReportingAPI.sln --packages /tmp/codeql-scratch-f294ada2fef3658c/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-f294ada2fef3658c/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-f294ada2fef3658c/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true EnableCommentTool=true;EnablePlanTool=true] (dns block)
    • Triggering command: /usr/bin/dotnet dotnet restore --no-dependencies /home/REDACTED/work/EstateReportingAPI/EstateReportingAPI/EstateReportingAPI/EstateReportingAPI.csproj --packages /tmp/codeql-scratch-f294ada2fef3658c/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-f294ada2fef3658c/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-f294ada2fef3658c/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[Complexity] Method ReportingManager::GetContract has 81 lines of code (limit is 50)</issue_title>
<issue_description>

Codacy ID: 9e377ff63f0942cb820ab6818ca7133

File: EstateReportingAPI.BusinessLogic/ReportingManager.cs
Line: 299
Rule: Complexity (Warning)
Commit: c6ec82b8138936189d33b99020db45b30d2d8343

Message: Method ReportingManager::GetContract has 70 lines of code (limit is 50)

**Code Snippet:**
```csharp
    public async Task<Result<Contract>> GetContract(ContractQueries.GetContractQuery request, 
```

</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…omplexity below 50 lines

Co-authored-by: StuartFerguson <[email protected]>
Copilot AI changed the title [WIP] Refactor GetContract method to reduce complexity Refactor: Extract LoadProductsWithFees to reduce GetContract complexity Feb 27, 2026
@StuartFerguson StuartFerguson marked this pull request as ready for review February 27, 2026 17:43
@StuartFerguson StuartFerguson merged commit f15ba22 into main Feb 27, 2026
8 checks passed
@github-actions github-actions Bot deleted the copilot/refactor-get-contract-method branch April 29, 2026 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Complexity] Method ReportingManager::GetContract has 81 lines of code (limit is 50)

2 participants