Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core 2 - Create instance of controller class which has a repository

I have the following controller Class;

public class MyController: Controller
{
    private IValueService _service;

    public MyController(IValueService service)
    {
        this._service = service;
    }

    public void MyMethod()
    {
        var values = _service.GetAll();
    }
}

I would like to create an instance of this Class and call the method MyMethod(), e.g.

var MyCopy = new MyController();
MyCopy.MyMethod();

However i get the message i need to give the required service parameter. How would i create an instance of a controller that has a service (repository) so i can call its methods?

like image 388
phicon Avatar asked Feb 05 '18 23:02

phicon


2 Answers

In Startup file add :

 services.AddMvc().AddControllersAsServices();  

it will registers all controllers in your application with the DI container.

Then :

   var serviceProvider = HttpContext.RequestServices;
     var controller =(MyController)serviceProvider.GetRequiredService<MyController>();
     controller.Method();
like image 93
Amer Jamaeen Avatar answered Nov 07 '22 09:11

Amer Jamaeen


Well, on startup I would like to run a method in a class that uses a repository. This method first opens a SocketIO connection and then it should use the repository to save new incoming data to the database.

Then that logic shouldn’t be inside a controller but in some service type instead which you register with the dependency injection container. Controllers are to respond to requests at certain routes, but your thing sounds like it’s a general initialization step in your application that runs outside of a request. So there should be no controller involved.

Instead, you want to make some service first:

public class MyInitializationService
{
    private readonly IValueService _valueService;

    public MyInitializationService(IValueService valueService)
    {
        _valueService = valueService;
    }

    public void Initialize()
    {
        var values = _valueService.GetAll();
        // do something
    }
}

You then register that class in the ConfigureServices method of your Startup class:

services.AddTransient<MyInitializationService>();

And then, you can inject that type into the Configure method and call its Initialize method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, MyInitializationService initializationService)
{
    initializationService.Initialize();

    // …
    app.UseStaticFiles();
    app.UseMvc();
}

There are various ways of running something at the beginning when an application starts. Calling it inside Configure is just one way, which may or may not be appropriate depending on how early you want your code to run (since Configure runs very early). Another good way would be to register a callback to the ApplicationStarted event of the application lifetime. I have an answer over here which goes into more details on that.

like image 38
poke Avatar answered Nov 07 '22 08:11

poke