When building an MVC application, it is very easy to keep adding code to a controller.
At first, the action looks harmless:
public IActionResult Create()
{
return View();
}
Then we add validation.
Then database code.
Then calculations.
Then business rules.
Then email logic.
Then suddenly the controller looks like it ate the entire application.
That is usually a sign that some logic belongs somewhere else.
One common place is a service class.
What Is a Controller Supposed to Do?
A controller sits between the HTTP request and the application.
A simplified MVC flow is:
Browser
↓
Controller
↓
Application Logic
↓
Database
A controller is good at things like:
- receiving request data
- checking
ModelState - calling application code
- choosing a View
- redirecting
- returning HTTP responses
What it should not become is one giant storage container for every rule in the system.
A Controller With Too Much Logic
Imagine this:
[HttpPost]
public async Task<IActionResult> Create(
CreateTransactionViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
if (model.Amount == 0)
{
ModelState.AddModelError(
"Amount",
"Amount cannot be zero.");
return View(model);
}
var account = await _context.Accounts
.FindAsync(model.AccountId);
if (account == null)
{
return NotFound();
}
account.Balance += model.Amount;
var transaction = new Transaction
{
AccountId = model.AccountId,
Amount = model.Amount,
Date = model.Date
};
_context.Transactions.Add(transaction);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
This is still understandable.
But what happens after six months of adding requirements?
Maybe now we need:
- transfer rules
- currency conversion
- category rules
- duplicate detection
- audit logging
- notifications
The controller can get ugly very quickly.
Move Business Logic Into a Service
I could create something like:
public interface ITransactionService
{
Task CreateTransactionAsync(
CreateTransactionViewModel model);
}
Then implement it:
public class TransactionService : ITransactionService
{
private readonly AppDbContext _context;
public TransactionService(AppDbContext context)
{
_context = context;
}
public async Task CreateTransactionAsync(
CreateTransactionViewModel model)
{
var account = await _context.Accounts
.FindAsync(model.AccountId);
if (account == null)
{
throw new InvalidOperationException(
"Account not found.");
}
account.Balance += model.Amount;
var transaction = new Transaction
{
AccountId = model.AccountId,
Amount = model.Amount,
Date = model.Date
};
_context.Transactions.Add(transaction);
await _context.SaveChangesAsync();
}
}
Now the controller can be smaller:
[HttpPost]
public async Task<IActionResult> Create(
CreateTransactionViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
await _transactionService
.CreateTransactionAsync(model);
return RedirectToAction("Index");
}
The Architecture Becomes Clearer
Instead of:
Controller
├── validation
├── calculations
├── database queries
├── business rules
├── notifications
└── everything else
we can move toward:
Browser
↓
Controller
↓
Service
↓
EF Core
↓
Database
Each piece has a clearer responsibility.
What Is Business Logic?
Business logic means the rules that make the application behave according to its purpose.
For a finance application, examples might include:
A transfer must have a source account.
A transaction amount cannot be zero.
A transfer should update two account balances.
A deleted account cannot accept a new transaction.
Those are not really web-page rules.
They are application rules.
That is why putting them into a service can make sense.
Why Does This Help With Testing?
Suppose all the transaction logic is buried inside a controller action.
Testing it means dealing with the controller and everything attached to it.
But if the main rule lives inside:
TransactionService
I can test that class directly.
Conceptually:
TEST
↓
TransactionService
↓
Does the rule work?
That is usually easier than:
TEST
↓
Controller
↓
MVC behavior
↓
Business logic
↓
Database behavior
Separation makes tests more focused.
Dependency Injection Connects the Pieces
The controller needs access to the service.
So I might register it:
builder.Services.AddScoped<
ITransactionService,
TransactionService>();
Then inject it:
private readonly ITransactionService
_transactionService;
public TransactionsController(
ITransactionService transactionService)
{
_transactionService = transactionService;
}
The flow becomes:
Dependency Injection
↓
Controller gets ITransactionService
↓
Controller calls service
↓
Service performs business logic
Now dependency injection starts to feel much more useful than just a strange constructor pattern.
Does Every Tiny App Need a Service Layer?
No.
I do not think every three-line controller action needs its own service.
Overengineering can make simple code harder to understand.
But when business rules start growing or need to be reused, a service can provide a much cleaner home for them.
A good question is:
Does this code describe how the web request works, or does it describe how the business/application works?
If it is mostly application behavior, I would at least consider moving it out of the controller.
What I Would Say in an Interview
If someone asked:
“Why would you use a service layer?”
I would say:
A service layer can separate business logic from controllers and other UI concerns. That helps keep controllers focused on request and response handling, makes business rules easier to reuse, and can make the code easier to unit test and maintain.
My mental picture is:
Controller
Traffic cop
Service
Business rules
EF Core
Database access
A controller does not need to personally do every job.
Sometimes its job is simply to send the work to the right place.
