Skip to content

Add support for constructor injection in components#28

Merged
LittleLittleCloud merged 4 commits intoRazorConsole:mainfrom
DericHuynh:main
Oct 15, 2025
Merged

Add support for constructor injection in components#28
LittleLittleCloud merged 4 commits intoRazorConsole:mainfrom
DericHuynh:main

Conversation

@DericHuynh
Copy link
Contributor

Closes #25

This pull request introduces support for constructor injection for Razor Components by implementing a custom IComponentActivator. This change allows developers to use constructor injection for their dependencies, which is a more modern and robust pattern compared to relying solely on property injection with [Inject].

Description of Changes

As discussed in issue #25, the default IComponentActivator uses Activator.CreateInstance(type), which is limited to parameterless constructors. This prevents constructor injection.

This PR addresses this by:

  1. A new ComponentActivator is introduced that accepts an IServiceProvider in its constructor, it's a carbon copy of the default component activator except it uses ActivatorUtilities with IServiceProvider instead of Activator.
  2. Registering the ComponentActivator as a service so its discovered and used by the Renderer implementation.
  3. Adding a new xUnit test verify that the constructor injection functionality works as expected.

@DericHuynh DericHuynh marked this pull request as ready for review October 15, 2025 22:17
@LittleLittleCloud
Copy link
Member

LGTM, approved with minor feedback

Copy link
Contributor

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

This PR adds constructor injection support for Razor components by implementing a custom IComponentActivator that uses ActivatorUtilities instead of Activator.CreateInstance. This enables developers to inject dependencies through constructors rather than relying solely on property injection with [Inject] attributes.

Key Changes

  • Introduced ComponentActivator class that leverages ActivatorUtilities for service-aware component instantiation
  • Registered ComponentActivator as a singleton service in the DI container
  • Added comprehensive test coverage including keyed services, error scenarios, and basic constructor injection

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/RazorConsole.Core/Utilities/ComponentActivator.cs New activator implementation supporting constructor injection via ActivatorUtilities
src/RazorConsole.Core/AppHost.cs Registers ComponentActivator in DI container and removes unused imports
src/RazorConsole.Tests/ComponentActivatorTests.cs Comprehensive test suite verifying constructor injection, keyed services, and error handling

@LittleLittleCloud LittleLittleCloud merged commit bbedb4b into RazorConsole:main Oct 15, 2025
3 checks passed
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.

Feature Request: Support Constructor Injection for Razor Components

3 participants