Class Images
Class Code
ASP.NET Core MVC Architecture - In-Depth Notes
Overview
ASP.NET Core MVC follows the Model-View-Controller architectural pattern, separating application logic into three interconnected components. This promotes separation of concerns, testability, and maintainability.
1. Program.cs - Application Entry Point
WebApplication Builder Pattern
var builder = WebApplication.CreateBuilder(args);
- WebApplicationBuilder: Factory pattern for configuring services and middleware
- args: Command-line arguments passed to the application
- Creates the host, configures services, and builds the application pipeline
Service Registration & Dependency Injection
Service Container
ASP.NET Core has a built-in IoC (Inversion of Control) container for dependency injection:
// Different service lifetimes demonstrated in your code:
// SCOPED - One instance per HTTP request
builder.Services.AddScoped(typeof(ISpellChecker), typeof(EnglishSpellChecker));
// SINGLETON - One instance for application lifetime
builder.Services.AddSingleton(typeof(ISpellChecker), typeof(EnglishSpellChecker));
// TRANSIENT - New instance every time requested (not shown but available)
builder.Services.AddTransient<ISpellChecker, EnglishSpellChecker>();
Service Lifetimes Explained
- Singleton: Created once, shared across all requests
- Use for: Stateless services, configuration, caching
- Memory efficient but must be thread-safe
- Scoped: Created once per HTTP request
- Use for: Database contexts, user-specific services
- Default for most business services
- Transient: Created every time requested
- Use for: Lightweight, stateless services
- Most memory intensive
MVC Services Registration