Last active
April 20, 2021 20:28
-
-
Save manoj-choudhari-git/03abfe48afac528a01adaeec33686ccc to your computer and use it in GitHub Desktop.
Options Pattern Bind vs Get vs Configure in DI container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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