An Application Controller is responsible for handling the overall flow of the application. It coordinates multiple use cases and manages the high-level application logic.
Key Characteristics:
- Scope: Manages the flow of the entire application or a significant part of it.
- Granularity: Coarser-grained, dealing with broader application workflows.
- Responsibility: Orchestrates multiple use cases, manages session state, and handles navigation between different parts of the application.
Example: In a web application, an ApplicationController that manages user authentication and session state can be considered an Application Controller. Below is an example of an ApplicationController for a Rails app.
class ApplicationController < ActionController::Base
before_action :authenticate_user!
private
def authenticate_user!
unless current_user
redirect_to login_path, alert: "Please log in to continue."
end
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end