Per Petter
Per Petter

Reputation: 71

Reusing code in controllers

I have a block of code which is use in pretty much every controller, so I am wondering how, or what's the best practice for reusing code in multiple controllers

Simple example would be this

public String CoolCode(){
    // Stuff
    return MyStuff;
}

Then in another controller I just use

string something = CoolCode();

Where should I put it, and how to use it in every controller?

Upvotes: 1

Views: 1999

Answers (4)

Snger
Snger

Reputation: 1384

public interface IBaseUserController
{
    string SomePropety { get; set; }

    ActionResult SignUp(string code, [Form] SomeViewModel model);
}

public class BaseUserController : Controller, IBaseUserController
{
    private static string _somePropety = "";

    public BaseUserController(){}

    public string SomePropety
    {
        get
        {
            return _somePropety;
        }
        set { _somePropety = value; }
    }

    public virtual ActionResult SignUp(string code, [Form] SomeViewModel model)
    {
        // ... CoolCode maybe use SomePropety
        return View(model);
    }
}
public class TestUserController : BaseUserController
{
    public TestUserController()
    {
        SomePropety = "Value";
    }

    public override ActionResult SignUp(string code, [Form] SomeViewModel model)
    {
        return base.SignUp(code, model);
    }

    public ActionResult SignUp2(string code, [Form] SomeViewModel model)
    {
        return base.SignUp(code, model);
    }
}

Upvotes: 1

nik0lai
nik0lai

Reputation: 2655

Personally I would inject the helper class into the controller:

public interface IHelper
{
    string CoolCode();
}

public class Helper : IHelper
{
    public string CoolCode()
    {
        return "Cool code";
    }
}

public class SomeController
{
   private IHelper _helper;
   public SomeController(IHelper helper)
   {
       _helper = helper;
   }

   public ActionResult Index()
   {
       //call _helper.CoolCode();
   }
}

Then you would need to inject this using some sort of IoC container, I recommend Castle Windsor

This is all quite abstract but I recommend you read up on it:

http://www.codeproject.com/Articles/560798/ASP-NET-MVC-Controller-Dependency-Injection-for-Be

Upvotes: 4

DeepakJ
DeepakJ

Reputation: 388

MVC controller are same as normal class and controller have the same extension .cs

So use can use static method like following.

HomeController objHomeController = new HomeController();
string something=  objHomeController.CoolCode();

Upvotes: 0

astian
astian

Reputation: 684

What you should do is extend the Controller class that all your controllers inherit now and use this abstraction to wrap all of your controllers that use that code:

public class MyControllerBase : Controller
{
   public string CoolCode() { ... } 
}

now you simply inherit your abstraction rather than the default one:

public class AnyController : MyControllerBase
{
...
}

However depending on what you need precisely different approaches might be more appropriate.

Upvotes: 1

Related Questions