module-di

Configure Magento 2 dependency injection preferences, virtual types, and proxies in di.xml.

2|3|Updated Mar 19, 2026
One-click install
npx skills add https://github.com/caravanglory/magehub --skill module-di
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: module-di
Source: https://github.com/caravanglory/magehub/tree/main/skills/module/module-di
Command: npx skills add https://github.com/caravanglory/magehub --skill module-di

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you configure and troubleshoot Magento 2 dependency injection so modules use the right implementations, constructor arguments, and scoped bindings without direct ObjectManager calls.

Core Features & Use Cases

  • Interface Preferences: Bind service contracts to concrete implementations in global or area-scoped di.xml files.
  • Virtual Types and Proxies: Adjust constructor arguments and defer expensive dependencies without creating unnecessary subclasses.
  • DI Troubleshooting: Diagnose compile errors, circular dependencies, and incorrect wiring in Magento modules.
  • Use Case: When a Magento module needs a different logger, data provider, or repository implementation in adminhtml versus frontend, this Skill guides the safest DI configuration.

Quick Start

Ask this Skill to review your Magento 2 dependency injection setup and tell you exactly what to change in di.xml and constructors.

Frequently Asked Questions about module-di

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I configure Magento 2 dependency injection without using ObjectManager?

Magento 2 dependency injection without ObjectManager requires configuring interface preferences, constructor arguments, and scoped bindings in di.xml files. You bind service contracts to concrete implementations and wire dependencies through constructor injection.

Why does my Magento 2 module have a circular dependency or compile error in di.xml?

Circular dependency and compile errors in di.xml often stem from incorrect constructor wiring or missing interface preferences. Diagnose these Magento 2 dependency injection issues by checking virtual type argument overrides and scoped configuration bindings.

When do I need to use proxies and virtual types in Magento 2 dependency injection?

Proxies and virtual types in Magento 2 dependency injection are needed when you want to defer expensive dependencies or adjust constructor arguments without creating unnecessary subclasses. They optimize resource loading during module compilation.

How do I set different dependency injection bindings for adminhtml versus frontend in Magento 2?

To set different dependency injection bindings for adminhtml versus frontend, configure area-scoped di.xml files within your Magento 2 module. This allows specific implementations, like different loggers or repositories, to load based on the application area.

Can I override constructor arguments for a specific Magento 2 module without modifying the original class?

Yes, you can override constructor arguments for a specific Magento 2 module without modifying the original class by defining virtual types and argument overrides in your di.xml configuration. This ensures safe dependency injection customization.

What is the best way to bind a service contract interface to a concrete implementation in Magento 2?

The best way to bind a service contract interface to a concrete implementation in Magento 2 is by defining a preference in your di.xml file. This dependency injection configuration explicitly maps the interface to the target class.