3-Tier Layered Architecture
┌──────────────────────┐ │ Presentation (UI) │ ├──────────────────────┤ │ Business Logic │ ├──────────────────────┤ │ Data Access Layer │ ├──────────────────────┤ │ Database │ └──────────────────────┘
Upper → Lower only। Lower → Upper আনলে coupling বাড়ে।
উদাহরণ
// Presentation
public class OrderController : ControllerBase {
IOrderService _service;
[HttpPost] public async Task<IActionResult> Create(Dto dto) {
var id = await _service.PlaceAsync(dto);
return Created(...);
}
}
// Business
public class OrderService : IOrderService {
IOrderRepository _repo;
IPaymentGateway _payment;
public async Task<int> PlaceAsync(Dto dto) {
var order = new Order(dto);
await _payment.ChargeAsync(order.Total);
return await _repo.AddAsync(order);
}
}
// Data
public class OrderRepository : IOrderRepository {
AppDbContext _ctx;
public Task<int> AddAsync(Order o) {...}
}
MVC ⭐
User input → Controller → Model
↓ ↓
View ←──────┘
MVP
View passive, Presenter সব logic।
MVVM ⭐ (WPF, Vue, Angular)
Model ←→ ViewModel ←→ View
two-way binding
Repository Pattern
public interface IUserRepository {
Task<User?> GetByIdAsync(int id);
Task AddAsync(User u);
void Update(User u);
}
Unit of Work
public interface IUnitOfWork {
IUserRepository Users { get; }
IOrderRepository Orders { get; }
Task<int> CommitAsync();
}
Dependency Injection
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IOrderService, OrderService>();
public class OrderService {
public OrderService(IUserRepository u, IOrderRepository o) {}
}
DI Lifetimes
- Singleton — পুরো জীবনে একটি
- Scoped — per request
- Transient — প্রতি resolve-এ নতুন
সুবিধা
Loose coupling, easy unit test (mock), config centralize, SOLID-D principle।