1

I have created one method in a controller, but I need to call this method into anther class and I don't know how to call it.

my sample code :-

  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
        }

    }

I have tried many things to call my method in another class method but I can't get it to work.

Sangeet Shah
  • 3,079
  • 2
  • 22
  • 25
  • 4
    `new ReportController().Reporting();` –  Apr 25 '16 at 07:57
  • Possible duplicate of [How to make method call another one in classes C#?](http://stackoverflow.com/questions/16226444/how-to-make-method-call-another-one-in-classes-c) – Draken Apr 25 '16 at 08:32
  • Is it possible for you to create Reporting method as static? – Mukund Apr 25 '16 at 09:54

3 Answers3

4

It's a bad design way to have some code in a controller who have to be called in several places. Create a class in your project or in an external DLL who contains this code and call this class in the controllers who need this method.

Create a class somewhere (in you project, or in a class library) :

public class MyClass
{
    public MyClass()
    {}

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

And implement this code in your controllers or classes who need this.

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

By the way, if your code doesn't need any instance, you can create a static class, like :

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();
        }
}
AdrienTorris
  • 9,111
  • 9
  • 34
  • 52
2

There are two ways of accomplishing this. You are using dependency injection in your controller's constructor, so you have two options of doing this (not sure if I have missed any?):

  • Create an instance of each of your services in the calling method and pass these instances through when instantiating the controller, or
  • Add a constructor to your home class, set the services and pass them through when instantiating the controller

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

It works on the same principle as creating an instance of a class and then calling its methods.

Option 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();
     }
}
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
  • But it's not a good idea to put methods in controller which can be used at multiple places. – Mukund Apr 25 '16 at 09:57
0
ReportController reportController = new ReportController();
reportController.Reporting();

As you would any other class that's method isn't static

Please read this answer, they go into a lot more detail than I do

Community
  • 1
  • 1
Draken
  • 3,134
  • 13
  • 34
  • 54
  • in parameterize contructure i cant crate object of controller and also i have implement BaseController's method into Report Controller so i can't create object on it directly – Sangeet Shah Apr 25 '16 at 09:45
  • you should use DI Library or create an new instance for each param , you can use `StructureMap ` its grate – Uthman Rahimi Apr 25 '16 at 09:49