Skip to content

Update AV1125: Don't expose stateful objects through static members#353

Merged
dennisdoomen merged 3 commits into
developfrom
copilot/pr298-update-av1125
May 9, 2026
Merged

Update AV1125: Don't expose stateful objects through static members#353
dennisdoomen merged 3 commits into
developfrom
copilot/pr298-update-av1125

Conversation

@dennisdoomen
Copy link
Copy Markdown
Owner

This PR updates guideline AV1125.

It was split out of #298 so the change can be reviewed independently.

Files:

  • _rules/1125.md

Part of the replacement for #298.

Split from #298.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread _rules/1125.md Outdated
A stateful object is an object that contains many properties and lots of behavior behind it. If you expose such an object through a static property or method of some other object, it will be very difficult to refactor or unit test a class that relies on such a stateful object. In general, introducing a construct like that is a great example of violating many of the guidelines of this chapter.

A classic example of this is the `HttpContext.Current` property, part of ASP.NET. Many see the `HttpContext` class as a source of a lot of ugly code.
A classic example of this is the `HttpContext.Current` property, part of classic ASP.NET. Many see this as a source of a lot of ugly code, because it hides an implicit dependency on a global state object. Instead, make dependencies explicit by injecting them through a constructor or method parameter.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to refer to classic ASP.NET; there are plenty of cases in modern .NET that are still a pain when writing tests:

  • System.Environment.MachineName
  • System.Environment.GetEnvironmentVariable (can't run tests in parallel that depend on it)
  • System.DateTime.UtcNow
  • System.IO.Path.Combine (depends on OS-level directory separator)
  • System.IO.Path.GetInvalidFileNameChars
  • System.IO.File.Exists

The rule title could be changed to:
Don't expose global state through static members

- Rename title to 'Don't expose global state through static members'
- Replace classic ASP.NET example with modern .NET examples
  (Environment.MachineName, DateTime.UtcNow, File.Exists, etc.)
- Note testability issues, especially parallel test execution
- Suggest TimeProvider as a modern abstraction over DateTime.UtcNow

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 9, 2026 12:39
- Broaden title to 'Don't hide dependencies behind static members'
  to cover both global state and environmental dependencies
- Clarify the closing sentence to emphasize injection of an abstraction
  rather than vague 'well-known abstractions'

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates guideline AV1125 to focus on avoiding static members that expose global/environment-dependent state, using concrete .NET examples and steering readers toward dependency injection/abstractions for testability.

Changes:

  • Renames the guideline title to emphasize “global state” rather than “stateful objects”.
  • Replaces the previous paragraph with examples of problematic static/global dependencies and a short rationale.
  • Adds a recommendation to inject or abstract these dependencies (e.g., TimeProvider).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread _rules/1125.md
Comment on lines +4 to +7
title: Don't hide dependencies behind static members
severity: 2
---
A stateful object is an object that contains many properties and lots of behavior behind it. If you expose such an object through a static property or method of some other object, it will be very difficult to refactor or unit test a class that relies on such a stateful object. In general, introducing a construct like that is a great example of violating many of the guidelines of this chapter.
A well-known example is `HttpContext.Current` from classic ASP.NET. But modern .NET still has plenty of cases where static members expose global or environment-dependent state, such as `Environment.MachineName`, `DateTime.UtcNow`, `Environment.GetEnvironmentVariable`, and `File.Exists`. These are problematic because:
Comment thread _rules/1125.md
- They make unit testing difficult, especially running tests in parallel.

A classic example of this is the `HttpContext.Current` property, part of ASP.NET. Many see the `HttpContext` class as a source of a lot of ugly code.
Instead, make dependencies explicit by injecting them through a constructor or method parameter, or by injecting an abstraction (e.g., `TimeProvider` instead of calling `DateTime.UtcNow` directly).
@dennisdoomen dennisdoomen merged commit 4665e9f into develop May 9, 2026
@dennisdoomen dennisdoomen deleted the copilot/pr298-update-av1125 branch May 9, 2026 12:44
dennisdoomen added a commit that referenced this pull request May 14, 2026
* Make sure the site works properly with Ruby 3.1

* Add AV0100: Understand the boundaries of your codebase (#300)

* Add AV0105: Use design patterns to communicate intent (#301)

* Add AV0110: Prefer composition over class inheritance (#302)

* Add AV0110 guideline

Split from #298.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update _rules/0110.md

Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

---------

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

* Add AV0112: Apply the Principle of Least Surprise (#303)

* Add AV0115 guideline (#304)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add AV0120: You Ain't Gonna Need It (YAGNI) (#305)

* Add AV0120 guideline

Split from #298.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update _rules/0120.md

Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

* Update _rules/0120.md

Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

* Update _rules/0120.md

Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

---------

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

* Add AV0125: Don't Repeat Yourself (DRY) within boundaries (#306)

* Update explanation of code boundaries and coupling

Clarified the reasoning for preventing unnecessary coupling between components.

* Add AV0135 guideline (#308)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add AV1582 guideline (#318)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add AV1608 guideline (#323)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add AV1610 guideline (#324)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1708 guideline (#372)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1562 guideline (#368)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1800 guideline (#375)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1521 guideline (#361)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1520 guideline (#360)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1140 guideline (#356)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Remove AV1530: Don't change a loop variable inside a `for` loop or a collection in a `foreach` loop (#363)

* Update AV1530 guideline

Split from #298.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Address review comments on AV1530: update title and add foreach collection example

Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dennisdoomen/CSharpGuidelines/sessions/983488b6-53b6-4360-876c-4576219b2d4f

* Remove AV1530 rule file entirely

Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dennisdoomen/CSharpGuidelines/sessions/0ce85de6-5278-491a-87c7-607ef017b67b

---------

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>

* Update AV1250 guideline (#358)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1115 guideline (#352)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1025 guideline (#351)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1020 guideline (#350)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1011 guideline (#348)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1010 guideline (#347)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1003 guideline (#345)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add AV1618 guideline (#326)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add AV1615: Prefer inline literals over constant variables in tests (#325)

* Add AV1615 guideline

Split from #298.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update _rules/1615.md

Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

---------

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

* Update AV1835 guideline (#378)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1825 guideline (#377)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update AV1000: A class or interface should have a single purpose (#344)

* Update AV1000 guideline

Split from #298.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update _rules/1000.md

Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

---------

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

* Remove AV2235 guideline (#342)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add AV1602: Postfix test classes with `Specs` instead of `Tests` (#321)

* Add AV1602 guideline

Split from #298.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Rename nested class Order_placement to OrderPlacement

Renamed nested class for better readability.

---------

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add AV1605: Test behavior, not implementation details (#322)

* Add AV1605 guideline

Split from #298.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update _rules/1605.md

Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

---------

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

* Add General Guidelines page (#385)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add Testability Guidelines page (#386)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update Links & Articles page (#387)

* Update C ov er An dS ty le s page (#384)

* Update AV2400 guideline (#382)

* Split minor site updates from PR 298 (#383)

* Add AV1600 guideline (#320)

* Add AV1578 guideline (#317)

* Update AV1706: Don't use abbreviations (#371)

* Update AV1701 guideline (#370)

* Update AV1755: Only use `Async` or `TaskAsync` as a suffix when a method has both synchronous and asynchronous versions (#374)

* Clarify suffix usage for Async methods (#393)

Follow-up for #374, to align with the replacement of "version" with "variant".

* Update AV2305: Document all `public`, `protected` and `internal` types and members (#381)

* Update AV2202: Prefer language syntax over explicit calls to underlying implementations (#379)

* Update AV1820: Only use `async` for I/O-bound or long-running activities (#376)

* Remove AV1580 guideline (#392)

* AV0125: Consider duplicating simple logic across modules to reduce coupling (#369)

* Update AV1554 guideline (#367)

* Update AV1553 guideline (#366)

* Update AV1546 guideline (#365)

* Update AV1545: Don't use an `if`-`else` construct instead of a simple (conditional) assignment (#364)

* Update AV1523 guideline (#362)

* Update AV1500: Methods should not exceed 15 statements (#359)

* Remove AV1230 guideline (#357)

* Update AV1137: Keep parameters as specific and narrow as possible (#355)

* Update AV1130: Return interfaces to unchangeable collections (#354)

* Update AV1125: Don't expose stateful objects through static members (#353)

* Update AV1013: Don't cast a base class to one of its derived classes (#349)

* Add LSP example with polymorphism to AV1011 (#396)

* Update AV1004: Use an interface rather than a base class to support multiple implementations (#346)

* Remove AV2307 guideline (#343)

* Remove AV2221 guideline (#341)

* Remove AV2201 guideline (#339)

* Remove AV1738 guideline (#338)

Split from #298.

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Remove AV1737 guideline (#337)

* Remove AV1568 guideline (#336)

* Remove AV1532: Avoid nested loops (#334)

* Remove AV1525 guideline (#333)

* Remove AV1510 guideline (#332)

* Remove AV1220 guideline (#331)

* Add AV2308: Document what a member tries to do, not what it does or how it does it (#330)

* Add AV2225: Use deconstruction to simplify variable assignments (#329)

* Add AV1622: Test concrete implementations as part of a larger integration scope (#328)

* Add AV1622 guideline

Split from #298.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update _rules/1622.md

Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

---------

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>

* Add AV1620: Test reusable components separately from their consumers (#327)

* Add AV1585: Make properties required when they must be set during initialization (#319)

* Add AV1155: Use the `field` keyword in auto-properties when additional logic is needed (#316)

* Add AV1150: Avoid local functions (#315)

* Add AV1145: Use extension members to add behavior without modifying the original type (#314)

* Add AV1035: Use primary constructors when they improve readability (#313)

* Add AV1032: Consider a delegate instead of an interface with a single method (#312)

* Add AV1030: Know when to use a record and when to use a class (#311)

* Add AV1002: Only pass things to a constructor that most or all members need (#309)

* Remove AV2207 guideline (#340)

* Add AV0130: Apply the three pillars of object-oriented programming (#307)

* Fix c# fencing to use csharp in assertion-comparison.html

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Remove severity field from all rules

The severity rating is no longer used. This removes:
- The severity front matter field from all 130 rule files
- The severity icon <img> from the rule-category layout
- The unused severity images (1.png, 2.png, 3.png)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Dennis Doomen <dennis.doomen@greenchoice.nl>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Bart Koelman <10324372+bkoelman@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants