An Input Controller is a design pattern described by Martin Fowler in his book “Patterns of Enterprise Application Architecture.” It is responsible for handling user input and directing it to the appropriate application logic. The Input Controller pattern centralizes the control logic for processing user inputs, making it easier to manage and maintain. The Input Controller is the C in the Model-View-Controller pattern.
Key Characteristics:
- Scope: Typically handles input for a specific use case or a small set of related use cases.
- Granularity: Finer-grained, often dealing with individual user actions or requests.
- Responsibility: Validates input, invokes business logic, and handles responses for specific actions.
Key Responsibilities:
- Receive User Input: The Input Controller captures user input from various sources, such as web forms, APIs, or command-line interfaces.
- Validate Input:** It performs validation to ensure the input data is correct and complete.
- Invoke Business Logic: The controller invokes the appropriate business logic or service layer to process the input.
- Handle Responses: It prepares and sends the response back to the user, which could be a web page, JSON data, or a command-line output.
In a Ruby on Rails application, the ArticlesController can be considered an example of an Input Controller. It handles HTTP requests, validates input, invokes business logic, and renders responses.
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
The Input Controller pattern can be broken down into two separate patterns: Page Controller and the Front Controller