Ali
Ali

Reputation: 2768

Best practice ASP.NET Controller for Common Actions

I have a quick question,

In so many examples I saw controller Actions which does not return any View or Partialview are also added in the same controller - Now in my situation i have multiple controllers which i need to run same ACTIONS such as

Adding Action A in both Controller A and B does not look right.

As I am making [HttpPost] calls for Action A using AJAX, therefore, i want this to be in a Controller for easy POST access, although I don't want to add the same Action in both Controllers

I can add action A in Controller A and let Controller B access the same action requesting from Controller A instead but what I was thinking

**How about if I create a new Controller called

commonActionContoller

and put "ACTION A" in it and let everything use commonActionContoller when Action A is required?**

Cheers

EDIT: Example added as following

An example application which has Person and Countries, So Person model is the same for all Countries but we have different Controller for each country so If an Admin wants to update Person's model with field IsEmpoyed from true to false then they go to for example {USA}/Index controller and switch true to false. Now, this is same for {AUS}/Index and {China}/Index so Action which changes IsEmpyed to true/false is the same across all controllers. To make this work i don't want to add Action IsEmplyed to all country controllers - (Couldn't think of a better example) –

Upvotes: 3

Views: 2046

Answers (2)

Jaber
Jaber

Reputation: 277

You should write Action A in both Controller. Otherwise it will violate Single responsibility principle. Best practice is to move the implementation codes from Controller to a Service layer. For example, if you want to load Product Category for Products and Sub-Categories, then code will be like this:

public interface ICategoryService
{
    List<Category> LoadCategory();
}

public class CategoryService : ICategoryService
{
    public List<Category> LoadCategory()
    {
        //code here
    }
}

public class ProductController : Controller
{
    private readonly ICategoryService _categoryService;
    public ProductController()
    {
        _categoryService = <inject dependency here>;
    }

    public ActionResult GetCategory()
    {
        var category = _categoryService.LoadCategory();
    }
}

public class SubCategoryController : Controller
{
    private readonly ICategoryService _categoryService;
    public SubCategoryController()
    {
        _categoryService = <inject dependency here>;
    }

    public ActionResult GetCategory()
    {
        var category = _categoryService.LoadCategory();
    }
}

Upvotes: 1

Austin T French
Austin T French

Reputation: 5131

The guiding principle here should be Separation Of Concerns.

If ControllerA and ControllerB have specific business logic, and adding a CommonActions controller give shared data a good isolated home this is a good practice.

Without a better illustration of your needs though it's difficult to answer.

A slightly better example might be the order application:

InventoryController EmployeeController

You probably don't want a CommomController with methods like:

GetStoreClosingHours(int storeNumber);
GetTotalSales(int employeeId);
GetEmployeeComps(int employeeId);

IoC and dependency injection might pay off as well depending on the actions. Where any controller could call methods like:

GetLastLogonTime(thisEmployee);

It's really a set of principles designing your application after all, and best practices aren't always super neatly packaged. I'd say most importantly choose something flexible, scalable and then stick with it.

Upvotes: 0

Related Questions