Update AV1125: Don't expose stateful objects through static members#353
Open
dennisdoomen wants to merge 1 commit intodevelopfrom
Open
Update AV1125: Don't expose stateful objects through static members#353dennisdoomen wants to merge 1 commit intodevelopfrom
dennisdoomen wants to merge 1 commit intodevelopfrom
Conversation
Split from #298. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
bkoelman
reviewed
Mar 29, 2026
| 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. |
Collaborator
There was a problem hiding this comment.
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.MachineNameSystem.Environment.GetEnvironmentVariable(can't run tests in parallel that depend on it)System.DateTime.UtcNowSystem.IO.Path.Combine(depends on OS-level directory separator)System.IO.Path.GetInvalidFileNameCharsSystem.IO.File.Exists
The rule title could be changed to:
Don't expose global state through static members
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR updates guideline AV1125.
It was split out of #298 so the change can be reviewed independently.
Files:
Part of the replacement for #298.