Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Last active April 20, 2021 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/03abfe48afac528a01adaeec33686ccc to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/03abfe48afac528a01adaeec33686ccc to your computer and use it in GitHub Desktop.
Options Pattern Bind vs Get vs Configure in DI container
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Some code...
MailFeature mailFeature = new();
Configuration.GetSection("MailFeature").Bind(mailFeature);
services.AddSingleton(mailFeature);
// Some other code....
}
}
// Inject dependency in controller.
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly MailFeature mailFeature;
// the Options class is directly injected
public HomeController(ILogger<HomeController> logger, MailFeature mailFeature)
{
_logger = logger;
this.mailFeature = mailFeature;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment