.NET Core 2-创建具有存储库的控制器类的实例 [英] .NET Core 2 - Create instance of controller class which has a repository

查看:91
本文介绍了.NET Core 2-创建具有存储库的控制器类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器类;

public class MyController: Controller
{
    private IValueService _service;

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

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

我想创建此类的一个实例,并调用方法MyMethod(),例如

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?

推荐答案


好,在启动时,我想在使用存储库的类中运行方法。此方法首先打开SocketIO连接,然后应使用存储库将新的传入数据保存到数据库。

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
    }
}

然后注册 Startup 类的 ConfigureServices 方法中的该类:

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

services.AddTransient<MyInitializationService>();

然后,您可以将该类型注入 Configure 方法并调用其 Initialize 方法:

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();
}

在应用程序启动时,可以通过多种方式在开始时运行某些内容。在 Configure 内部调用它只是一种方法,根据您希望代码运行多早(从 Configure开始, code>运行得非常早)。另一个好方法是将回调注册为应用程序生存期的 ApplicationStarted 事件。我在这里有一个答案,其中对此有更详细的说明。

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.

这篇关于.NET Core 2-创建具有存储库的控制器类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆