Rémy
Rémy

Reputation: 898

AspNet Core : how to get current controller object in a service

I built a .NetCore 2.1 project and have a base controller class like this

public class MyBaseController:Controller
{
    #region Properties

    protected readonly ILogger Logger;
    protected readonly VVDCCore.Tools.Interfaces.IResourceManager Resx; 

    protected IDictionary<string,string> ExtraParameters;

    #endregion

    #region CONSTRUCTOR

    public BaseController(ILogger<BaseController> logger,
        VVDCCore.Tools.Interfaces.IResourceManager resxManager)
    {
        Logger = logger;
        Resx = resxManager;
    }

    #endregion
}

All my controllers are child of this base class.

I have a service (ResourceServices which implements IResourceManager interface) which permits to retrieve a value depending of a key and the context.

I know how to access controller's name and action's name in the service thanks to IActionContextAccessor interface. But I would like to obtain current controller object. In the service, I need to access the property ExtraParameters from my baseController but don't know how to do that

To sum up I would like to do something like this

public class ResourcesServices{

    public ResourceServices([CurrentController] controller)
    {
        var extraParameters = ((MyBaseController)controller).ExtraParameters;
    }
}

Any way to this? TIA

Upvotes: 1

Views: 2882

Answers (1)

Fabio
Fabio

Reputation: 32445

Don't reference controller object to others classes.
Instead provide only information class requires.

public class ResourcesServices
{
    public ResourceServices(IDictionary<string, string> parameters)
    {
        var extraParameters = parameters;
    }
}

If you are using Dependency Injection and if ExtraParameters are known only during runtime (ResourcesServices instance can not be instantiated).
Then consider to pass parameters to the method of ResourcesServices which will use them.

public class ResourcesServices
{        
    public ResourceServices() { }

    public void DoSomething(IDictionary<string, string> parameters)
    {
        var extraParameters = parameters;
    }
}

When ResourceServices class requires "ExtraParameters" to be provided in constructor, you can create factory class which will be responsible for creating proper instance of ResourceServices with runtime values.

public class ResourcesServicesFactory
{        
    public ResourcesServicesFactory() { }

    public ResourcesServices Create(IDictionary<string, string> parameters)
    {
        return new ResourcesServices(parameters);
    }
}   

public class ChildController : MyBaseController
{
    private readonly ResourceServiceFactory _factory;


    public ChildController(  
        ILogger<BaseController> logger,
        VVDCCore.Tools.Interfaces.IResourceManager resxManager,
        ResourceServiceFactory factory) : base(logger, resxManager)
    {
        _factory = factory;
    }

    public IActionResult Get()
    {
        var service = _factory.Create(ExtraParameters);
        service.DoSomething();
    }
}

Consider controller as an Entry point of your application, which main responsibility pass "input" to the service and return output to the client.

Upvotes: 3

Related Questions