Dependency Injection
Dependency Injection
Section titled โDependency Injectionโvar builder = WebApplication.CreateBuilder(args);builder.Services.AddScoped<IMySvc, MySvc>();var app = builder.Build();
app.MapGet("/", (IMySvc svc) => svc.Hello());
app.Run();
public interface IMySvc { string Hello(); }public class MySvc : IMySvc { public string Hello() => "hi"; }
Notes
- Prefer constructor injection; avoid service locator pattern.