简体   繁体   English

如何将另一个 class 实例传递给 controller 构造函数

[英]How to pass another class instance to a controller constructor

I am very new to the controller model concept.我对 controller model 概念非常陌生。 I am not understanding how and where to call the constructor of HomeController and how to pass student class instance?我不明白如何以及在哪里调用 HomeController 的构造函数以及如何传递学生 class 实例?

private IStudent _student;
public HomeController(IStudent student)
{
 _student= student;
 }

public interface IStudent 
{
 // Some method
 }

Maybe, the functionality of services.AddTransient(IStudent,Student<>) I want to understand.也许,我想了解 services.AddTransient(IStudent,Student<>) 的功能。

You do not call the constructor of a Controller yourself in your code.您不要在代码中自己调用 Controller 的构造函数。 This is what the ASP.NET Framework does whenever it needs an instance of the controller to perform an action that is defined in that controller.这就是 ASP.NET 框架在需要 controller 的实例来执行在 controller 中定义的操作时所做的事情。

Your HomeController is part of an ASP.NET application running on a (web) server.您的 HomeController 是在(Web)服务器上运行的 ASP.NET 应用程序的一部分。 A user can execute a function of this application by calling a specific URL.用户可以通过调用特定的 URL 来执行此应用程序的 function。

For example, if the HomeController contains an Action method like this:例如,如果 HomeController 包含这样的 Action 方法:

public ActionResult Info()
{
    // do something, then return result
    ...
    
    return View();
}

this method would be executed whenever an URL similar to http://<server>/Home/Info is called.每当调用类似于http://<server>/Home/Info的 URL 时,都会执行此方法。

If you need information from a student instance inside the controller, you can pass it via dependency injection in the constructor, as it already happens in your code snippet.如果您需要来自 controller 中的学生实例的信息,您可以通过构造函数中的依赖注入传递它,因为它已经在您的代码片段中发生。 For this to work, IStudent must be registered with the service container.为此,必须向服务容器注册 IStudent。 This is done in Startup.cs in the ConfigureServices method:这是在 Startup.cs 的 ConfigureServices 方法中完成的:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddTransient<IStudent,Student>();
    ...
}

Such a service may have different lifetimes:这样的服务可能有不同的生命周期:

  • Singleton: only one instance is created, which is used over and over again Singleton:只创建一个实例,反复使用
  • Scoped: in a web application, a scoped service is created once per client request范围:在 web 应用程序中,每个客户端请求创建一次范围服务
  • Transient: each time the service is requested from the service container, a new instance is created瞬态:每次从服务容器请求服务时,都会创建一个新实例

Depending on what lifetime you want your service to have, you need to choose the registration method accordingly.根据您希望服务拥有的生命周期,您需要相应地选择注册方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将类实例传递给基类构造函数 - How to pass class instance to base class constructor 如何将值传递给另一个程序集中的 class 的构造函数 - How to pass value to constructor of a class in another assembly 如何正确地将实例引用传递给另一个类的构造函数? - How to properly pass instance reference to another classes' constructor? 将Class的实例作为参数传递给Attribute构造函数 - Pass instance of Class as parameter to Attribute constructor 如何在C#中将一个类的实例传递给另一个类? - How to pass an instance of a class to another class in c#? 传递Controller构造函数中的接口或类以进行依赖项注入 - Pass Interface or Class in Controller Constructor for dependency injection Ihttpcontextaccessor在构造函数内的实用程序类中使用,如何在控制器中创建该类的实例 - Ihttpcontextaccessor used in a utility class inside a constructor, how do i create an instance of that class in the controller 如何动态地将多个对象实例传递给另一个类。 - How to dynamically pass multiple object instance to another class. NUnit TestFixtureAttribute:如何通过TestFixtureAttribute将DateTime实例传递给测试类构造函数 - NUnit TestFixtureAttribute: How do I pass a DateTime instance into the test class constructor via the TestFixtureAttribute 如何将路由参数传递给控制器​​构造函数 - How to pass routing parameter to controller constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM