7

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?

phicon
  • 3,549
  • 5
  • 32
  • 62
  • 1
    Controller is a normal class, the same as any other class. You must supply a value for service parameter like `var myservice = new ValueService(); var mycopy = new MyController(myservice);` – Al Kepp Feb 05 '18 at 23:54
  • 2
    No, no, no, you don't ever need to instantiate your own controllers, let the MVC framework do that for you. If you need to call a method, that method should be in it's own, non-controller class, preferably injected with the built in DI container. – DavidG Feb 05 '18 at 23:58
  • 1
    What dependency does your `IValueService` implementation have that prevents you from calling `service.GetAll()` directly? – Jasen Feb 06 '18 at 00:05
  • IValueSerive uses DataContext : DbContext – phicon Feb 06 '18 at 00:07
  • @DavidG - could you provide an example how to use the DI container with this class / method - could be the answer i am looking for (removing the controller bit) – phicon Feb 06 '18 at 00:08
  • 2
    This is just normal DI and normal object-oriented programming. Plenty of tutorials online, go look 'em up :) – DavidG Feb 06 '18 at 00:12
  • Then instantiate the DbContext. `IValueService service = new ValueService(new DataContext(options))`. Provide options as required by your application. – Jasen Feb 06 '18 at 00:34
  • Check out the [dependency injection topic](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection) in the documentation. – *Why* do you want to do this in the first place? Is this for unit testing? – poke Feb 06 '18 at 00:38
  • @poke - 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. New to a lot of concepts, so a bit lost what the correct structure would be. – phicon Feb 06 '18 at 00:51

2 Answers2

14

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();
Amer Jamaeen
  • 677
  • 8
  • 16
9

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.

poke
  • 369,085
  • 72
  • 557
  • 602