Skip to main content
A newer version of this page is available. .
All docs
V23.1

Dependency Injection in Controllers

  • 2 minutes to read

To obtain dependencies, the corresponding constructor of the Controller class must be decorated with the ActivatorUtilitiesConstructorAttribute.

Constructor Injection with IServiceProvider Parameter (Preferred Method)

If your application implements multiple Controllers that use dependency injection, we recommend that you use IServiceProvider as shown in the following example. This technique works faster compared to the one where you pass individual services as parameters to the Controller class constructor.

public class DependencyInjectionServiceProviderSampleController : Controller { 

    private SimpleAction simpleAction; 
    private readonly IServiceProvider serviceProvider; 

    // Use a constructor without parameters to initialize Actions.
    // This is necessary for the correct work of the Model Editor.
    public DependencyInjectionServiceProviderSampleController() { 
        simpleAction = new SimpleAction(this, "SimpleAction", PredefinedCategory.RecordEdit); 
        simpleAction.Execute += SimpleAction_Execute; 
    } 

    // Implement this constructor to support dependency injection.
    [ActivatorUtilitiesConstructor] 
    public DependencyInjectionServiceProviderSampleController(IServiceProvider serviceProvider) : this() { 
        this.serviceProvider = serviceProvider; 
    } 

    private void DoWork() { 
      var serviceOne = serviceProvider.GetRequiredService<IServiceOne>(); 
      var serviceTwo = serviceProvider.GetRequiredService<IServiceTwo>(); 
      //...  
    } 

} 

Important

The constructor without parameters (this()) is required for Model Editor operation.

By design, you cannot declare Actions or call the InitializeComponent() method in the constructor for dependency injection.

Constructor Injection with Individual Services As Parameters

Alternatively, you can pass services as parameters to the Controller class constructor.

public interface IServiceOne { } 
public interface IServiceTwo { } 

public class ConcreteServiceOne : IServiceOne { } 
public class ConcreteServiceTwo : IServiceTwo { } 

public class DependencyInjectionSampleController : Controller { 
    private SimpleAction simpleAction; 
    private readonly IServiceOne serviceOne; 
    private readonly IServiceTwo serviceTwo; 

    // Use a constructor without parameters to initialize Actions.
    // This is necessary for the correct work of the Model Editor.
    public DependencyInjectionSampleController() { 
        simpleAction = new SimpleAction(this, "SimpleAction", PredefinedCategory.RecordEdit); 
        simpleAction.Execute += SimpleAction_Execute; 
    } 

    // Implement this constructor to support dependency injection.
    [ActivatorUtilitiesConstructor] 
    public DependencyInjectionSampleController(IServiceOne serviceOne, IServiceTwo serviceTwo) : this() { 
        this.serviceOne = serviceOne; 
        this.serviceTwo = serviceTwo; 
    } 

} 
See Also