Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IControllerFactory vs IControllerActivator asp.net core

I have faced some kind of confusion with IControllerFactory and IControllerActivator. I was trying to create controllers manually because it is required by my project. So, I looked at the Microsoft documentation. However, descriptions for both interfaces are confusing.

Descriptions for each interface

IControllerFactory: Provides methods for the creation and disposal of controllers.

IControllerActivator: Provides methods to create a controller.

Both are saying that they provide methods to create controllers. So, if I want to create a controller manually, which interface should I use? Is there any difference between them?

like image 575
Harun Diluka Heshan Avatar asked Jun 06 '19 13:06

Harun Diluka Heshan


1 Answers

In order to create a controller instance, ASP.NET Core obtains an instance of IControllerFactory and uses it for controller creation.

However, if you look at the ASP.NET Core DefaultControllerFactory implementation, you will see that both the IControllerFactory and IControllerActivator are actually used for controller creation, because DefaultControllerFactory uses IControllerActivator to create an instance of the controller.

DefaultControllerFactory requires a dependency of type IControllerActivator to be passed in the constructor:

public DefaultControllerFactory(
    IControllerActivator controllerActivator,
    IEnumerable<IControllerPropertyActivator> propertyActivators)
{
    if (controllerActivator == null)
    {
        throw new ArgumentNullException(nameof(controllerActivator));
    }

    if (propertyActivators == null)
    {
        throw new ArgumentNullException(nameof(propertyActivators));
    }

    _controllerActivator = controllerActivator;
    _propertyActivators = propertyActivators.ToArray();
}

and CreateController and ReleaseController methods basically just invoke the IControllerActivator's corresponding methods:

public object CreateController(ControllerContext context)
{
    ... some null checks

    // _controllerActivator is of type IControllerActivator
    var controller = _controllerActivator.Create(context);
    foreach (var propertyActivator in _propertyActivators)
    {
        propertyActivator.Activate(context, controller);
    }

    return controller;
}
public void ReleaseController(ControllerContext context, object controller)
{
    ... some null checks

    _controllerActivator.Release(context, controller);
}

The only additional thing that the default instance of IControllerFactory does is invoking property activators (instances of IControllerPropertyActivator).

What you can do in your case?

  • Option 1: Provide your own implementation of IControllerFactory and create controller instances however you want. In this case IControllerActivator will not be used unless you require it.
  • Option 2: Use the default implementation of IControllerFactory, but provide your own implementation of IControllerActivator which will be used by DefaultControllerFactory.
like image 116
Marko Papic Avatar answered Oct 24 '22 05:10

Marko Papic