Stop Using IOptions Wrong in .NET!
dev.to·1d·
Discuss: DEV
🔄Syncthing
Preview
Report Post

Ever got confused about which one to use in your .NET app? You’re not alone! Let me break it down in simple words. 🧵


📌 IOptions - The Simple One

Think of it as: A sticky note you write once and never change

What it does: Loads your settings when the app starts, and that’s it!

Example:

public class NotificationSettings
{
public string SmtpServer { get; set; }
public int Port { get; set; }
public string DefaultSender { get; set; }
}

public class NotificationService
{
private readonly NotificationSettings _settings;

public NotificationService(IOptions<NotificationSettings> options)
{
_settings = options.Value;
}
}

Use it for: Stuff that never changes - like your SMTP server, database URL, or default sender email The catch: You change the …

Similar Posts

Loading similar posts...