需要将控制器的action方法调用到c#mvc中的另一个类中

时间:2016-04-25 07:56:04

标签: c# asp.net-mvc nopcommerce

我在控制器中创建了一个方法,但我需要将此方法调用到anther类中,我不知道如何调用它。

我的示例代码: -

  public class ReportController : BaseController
    {
        private readonly IPermissionService _permissionService;
        private readonly ICompaniesService _companyService;

       public ReportController(IPermissionService permissionService,
            ICompaniesService companyService)
       {
            this._permissionService = permissionService;
            this._companyService = companyService;
       }

        public void Reporting()
        {
            // code
        }
      }


    public class Home {

        public void Execute()
        {
            //I need to execute the Reporting method here
        }

    }

我已尝试过很多东西在另一个类方法中调用我的方法,但我无法让它工作。

3 个答案:

答案 0 :(得分:4)

在控制器中安装一些必须在多个地方调用的代码是一种糟糕的设计方法。在项目或包含此代码的外部DLL中创建一个类,并在需要此方法的控制器中调用此类。

在某处(在您的项目中或在类库中)创建一个类:

public class MyClass
{
    public MyClass()
    {}

    public void MyMethod()
    {
      // Some code here
    }
}

并在需要此控制器的控制器或类中实现此代码。

public class ReportController : BaseController
    {
        private readonly IPermissionService _permissionService;
        private readonly ICompaniesService _companyService;

       public ReportController(IPermissionService permissionService,
            ICompaniesService companyService)
       {
            this._permissionService = permissionService;
            this._companyService = companyService;
       }

        public void Reporting()
        {
            MyClass myClass = new MyClass();
            myClass.MyMethod();
        }
}

顺便说一句,如果你的代码不需要任何实例,你可以创建一个静态类,如:

public static class MyClass
{
    public static void MyMethod()
    {
       // Some code here
    }
}

public class ReportController : BaseController
{
       private readonly IPermissionService _permissionService;
       private readonly ICompaniesService _companyService;

       public ReportController(IPermissionService permissionService,
            ICompaniesService companyService)
       {
            this._permissionService = permissionService;
            this._companyService = companyService;
       }

        public void Reporting()
        {
            MyClass.MyMethod();
        }
}

答案 1 :(得分:2)

有两种方法可以实现这一目标。您正在控制器的构造函数中使用依赖注入,因此您有两种选择(不确定我是否错过了任何一种?):

  • 在调用方法中创建每个服务的实例,并在实例化控制器时传递这些实例,或者
  • 将一个构造函数添加到您的home类,设置服务并在实例化控制器时传递它们

选项1:

public class Home
{
     public void Execute()
     {
          // Create the variables that are needed by the constructor
          IPermissionService permissionService = new PermissionService();
          ICompaniesService companiesService = new CompaniesService();

          // Create an instance of your controller and pass through
          // the variables created above
          ReportController reportController = new ReportController(permissionService, companiesService);

          // Now that you have an instance of your controller call the method
          reportController.Reporting();
     }
}

它的工作原理与创建类的实例然后调用其方法的原理相同。

选项2:

public class Home
{
     private IPermissionService permissionService;
     private ICompaniesService companiesService;

     public Home(IPermissionService permissionService, ICompaniesService companiesService)
     {
          this.permissionService = permissionService;
          this.companiesService = companiesService;
     }

     public void Execute()
     {
          ReportController reportController = new ReportController(permissionService, companiesService);

          reportController.Reporting();
     }
}

答案 2 :(得分:0)

ReportController reportController = new ReportController();
reportController.Reporting();

正如你所做的任何其他类的方法都不是静态的

请阅读this回答,它们会比我更详细地了解