I’ll show a simple Addition Program in ASP.NET Core MVC using Model → Service → Controller → View architecture.
This is the best practice in MVC because the Controller should not contain business logic.
In ASP.NET Core MVC, a Service is a class that contains business logic and is used by the Controller to perform operations like calculation, database processing, API calls, etc.
👉 In simple words:
Service = Business Logic Layer
Instead of writing logic inside the controller, we create a Service class and call it from the controller.
1️⃣ What is Service (Example)
Service Class
public class AddService
{
public int AddNumbers(int a, int b)
{
return a + b;
}
}
Controller
public class HomeController : Controller
{
private readonly AddService _addService;
public HomeController(AddService addService)
{
_addService = addService;
}
public IActionResult Index()
{
int result = _addService.AddNumbers(10, 20);
ViewBag.Result = result;
return View();
}
}
1️⃣ Create Model (AdditionModel)
📁 Models/AddModel.cs
namespace AdditionApp.Models
{
public class AddModel
{
public int Num1 { get; set; }
public int Num2 { get; set; }
public int Result { get; set; }
}
}
This model stores:
First number
Second number
Result
2️⃣ Create Service Class
📁 Services/AddService.cs
using AdditionApp.Models;
namespace AdditionApp.Services
{
public class AddService
{
public int AddNumbers(int a, int b)
{
return a + b;
}
}
}
👉 Service layer contains business logic.
3️⃣ Register Service in Program.cs
📁 Program.cs
builder.Services.AddScoped<AddService>();
Now the service can be injected into controller using Dependency Injection.
4️⃣ Create Controller
📁 Controllers/HomeController.cs
using Microsoft.AspNetCore.Mvc;
using AdditionApp.Models;
using AdditionApp.Services;
namespace AdditionApp.Controllers
{
public class HomeController : Controller
{
private readonly AddService _addService;
public HomeController(AddService addService)
{
_addService = addService;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(AddModel model)
{
model.Result = _addService.AddNumbers(model.Num1, model.Num2);
return View(model);
}
}
}
Flow:
View → Controller → Service → Controller → View
5️⃣ Create View
📁 Views/Home/Index.cshtml
@model AdditionApp.Models.AddModel
<h2>Addition Program</h2>
<form method="post">
<label>Number 1</label>
<input type="number" asp-for="Num1" />
<br /><br />
<label>Number 2</label>
<input type="number" asp-for="Num2" />
<br /><br />
<button type="submit">Add</button>
</form>
@if (Model != null)
{
<h3>Result: @Model.Result</h3>
}
6️⃣ Complete MVC Flow
User enters numbers in View
↓
Controller receives model
↓
Controller calls Service
↓
Service performs addition
↓
Controller sends result to View
↓
View displays result
Flow:
View → Controller → Service → Controller → View
2️⃣ Why We Use Service
Benefits:
Clean Controller
Reusable Business Logic
Easy Testing
Better Project Architecture
Example tasks handled by services:
Calculations
Database operations
Payment processing
Email sending
API calls
3️⃣ Types of Service in ASP.NET Core (Dependency Injection Lifetime)
In ASP.NET Core, there are 3 main types of services based on lifetime.
1. Transient Service
🔹 Created every time when requested
builder.Services.AddTransient<AddService>();
Example:
Request 1 → New Object
Request 2 → New Object
Used for:
Lightweight services
Short operations
2. Scoped Service
🔹 Created once per HTTP request
builder.Services.AddScoped<AddService>();
Example:
One HTTP Request → Same Object
Next Request → New Object
Used for:
Database operations
Repository services
Most commonly used in MVC projects.
3. Singleton Service
🔹 Created only once for the entire application
builder.Services.AddSingleton<AddService>();
Example:
Application Start → One Object
All Requests → Same Object
Used for:
Configuration services
Caching
Logging
4️⃣ Quick Comparison
Service Type Object Creation
Transient New object every time
Scoped One object per request
Singleton One object for entire app
0 Comments
POST Answer of Questions and ASK to Doubt