Skip to content

Refactor Context: add IContext interface and register Context/IContext via DI - #256

Merged
michaelbeale-IL merged 7 commits into
masterfrom
copilot/refactor-context-class
Feb 23, 2026
Merged

Refactor Context: add IContext interface and register Context/IContext via DI#256
michaelbeale-IL merged 7 commits into
masterfrom
copilot/refactor-context-class

Conversation

Copilot AI commented Feb 22, 2026

Copy link
Copy Markdown
Contributor

Context was a static-only class with no DI registration, requiring all consumers to call Context.AppXXX directly and manually assign Context.ServiceProvider at startup.

Changes

New IContext interface (PanelManagement/IContext.cs)

Mirrors the full static Context API as instance members — all manager properties, configuration properties, and utility methods — so consumers can inject IContext and switch from static calls without renaming anything.

Context implements IContext

  • Adds Context(IServiceProvider) constructor that sets the static Context.ServiceProvider, wiring up DI on first resolution
  • Adds explicit IContext interface implementations delegating to static members — zero behavior change for existing Context.AppXXX callers

DI registration (ServiceCollectionExtensions.cs)

New AddContextService() registers IContext and Context as singletons. The factory (new Context(provider)) also configures Context.ServiceProvider, so the existing ResolveManager<T> fallback path continues to work. Included automatically via AddACATCoreModules().

// Before – manual wiring in Program.cs
_serviceProvider = services.BuildServiceProvider();
Context.ServiceProvider = _serviceProvider;

// After – Context is a proper DI citizen
_serviceProvider = services.BuildServiceProvider();
_serviceProvider.GetRequiredService<IContext>(); // sets ServiceProvider and registers singleton

Application startup (ACATApp, ACATTalk)

Replaced manual Context.ServiceProvider = ... assignment with resolving IContext from the container.

Tests (ContextDependencyInjectionTests.cs)

Three new tests: IContext resolves from DI, resolving it auto-configures Context.ServiceProvider, and Context/IContext share the same singleton instance.

Original prompt

This section details on the original issue you should resolve

<issue_title>Refactor Context Class</issue_title>
<issue_description>Remove static dependencies from Context class and inject services.

Acceptance Criteria:

  • Context class redesigned to use DI
  • All Context consumers updated
  • Static references removed or minimized
  • Proper lifetime management for Context
  • Tests updated
  • No breaking changes to external APIs

Technical Approach:

  • Inject required managers into Context
  • Provide Context through IServiceProvider
  • Update all Context.AppXXX references
  • Consider Context as scoped service

Risk Areas:

  • Many places reference Context statically
  • May need gradual migration approach
  • Consider adapter pattern for legacy code

Files to Modify:

  • Libraries/ACATCore/Utility/Context.cs
  • All files that use Context (search for 'Context.App')

Epic: Dependency Injection Infrastructure

Estimated Effort: 5 days


Parent Feature: #190 - Dependency Injection Infrastructure

</issue_description>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits February 21, 2026 20:37
…er, improve thread safety, update docs

Co-authored-by: michaelbeale-IL <63321611+michaelbeale-IL@users.noreply.github.com>
…update app startup

Co-authored-by: michaelbeale-IL <63321611+michaelbeale-IL@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor Context class to use dependency injection Refactor Context: add IContext interface and register Context/IContext via DI Feb 22, 2026
- Context singleton now always uses the root IServiceProvider, avoiding issues with scoped providers by introducing GetRootProvider.
- Added AddACATServices as an alias for AddACATCoreModules for clearer service registration.
- Added null check in GetWidgetTypeFullName to prevent exceptions when _widgetTypeCollection is null.
@michaelbeale-IL
michaelbeale-IL marked this pull request as ready for review February 23, 2026 15:50
@michaelbeale-IL
michaelbeale-IL merged commit c0dece6 into master Feb 23, 2026
2 checks passed
@michaelbeale-IL
michaelbeale-IL deleted the copilot/refactor-context-class branch February 23, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor Context Class

2 participants