Skip to content

Refactor read-model event dispatch to reduce Handle method complexity#1684

Merged
StuartFerguson merged 4 commits intomasterfrom
copilot/reduce-method-complexity
Mar 18, 2026
Merged

Refactor read-model event dispatch to reduce Handle method complexity#1684
StuartFerguson merged 4 commits intomasterfrom
copilot/reduce-method-complexity

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 18, 2026

ReadModelDomainEventHandler.Handle had grown beyond the configured method length threshold while acting as the top-level dispatcher for read-model updates. This change keeps the dispatch behavior intact but moves the iteration logic out of the public entrypoint so the method stays within the complexity limit.

  • Dispatcher extraction

    • Reduced Handle to a small entrypoint that either returns the matched handler task or falls back to Result.Success().
    • Moved the handler lookup loop into a private TryHandleDomainEvent helper using an explicit Try...out pattern.
  • Behavior-preserving fallback coverage

    • Added a focused test for an unhandled domain event to lock in the existing contract: no repository calls and a successful result when no handler matches.
  • Resulting shape

    • The handler map and per-domain switch expressions remain unchanged.
    • Only the dispatch orchestration was simplified.
public Task<Result> Handle(IDomainEvent domainEvent,
                           CancellationToken cancellationToken) =>
    this.TryHandleDomainEvent(domainEvent, cancellationToken, out Task<Result> handledTask)
        ? handledTask
        : Task.FromResult(Result.Success());

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: /usr/bin/dotnet dotnet test /home/REDACTED/work/TransactionProcessor/TransactionProcessor/TransactionProcessor.BusinessLogic.Tests/TransactionProcessor.BusinessLogic.Tests.csproj --filter ReadModelDomainEventHandlerTests --no-restore -nologo (dns block)
    • Triggering command: /usr/bin/dotnet dotnet test /home/REDACTED/work/TransactionProcessor/TransactionProcessor/TransactionProcessor.Tests/TransactionProcessor.Tests.csproj --filter VerifyBootstrapperIsValid --no-restore -nologo (dns block)
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp (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 ReadModelDomainEventHandler::Handle has 79 lines of code (limit is 50)</issue_title>
<issue_description>Codacy ID: c29a084936da782fe570473b0b917d08

File: TransactionProcessor.BusinessLogic/EventHandling/ReadModelDomainEventHandler.cs
Line: 20
Rule: Complexity (Warning)
Commit: 5b8f3e64d731c7b181b225e9d4117fda74faa3ac

Message: Method ReadModelDomainEventHandler::Handle has 79 lines of code (limit is 50)

**Code Snippet:**
```csharp
        public async Task<Result> Handle(IDomainEvent domainEvent, 
```

</issue_description>

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


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

Copilot AI and others added 2 commits March 18, 2026 13:00
Co-authored-by: StuartFerguson <16325469+StuartFerguson@users.noreply.github.com>
Co-authored-by: StuartFerguson <16325469+StuartFerguson@users.noreply.github.com>
Copilot AI changed the title [WIP] [Complexity] Refactor ReadModelDomainEventHandler::Handle method Refactor read-model event dispatch to reduce Handle method complexity Mar 18, 2026
Copilot AI requested a review from StuartFerguson March 18, 2026 13:06
@StuartFerguson StuartFerguson marked this pull request as ready for review March 18, 2026 13:51
@StuartFerguson StuartFerguson merged commit 6c61409 into master Mar 18, 2026
7 checks passed
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 ReadModelDomainEventHandler::Handle has 79 lines of code (limit is 50)

2 participants