DEV Community

Cover image for ๐Ÿš€ ๐ˆ๐Ž๐ฉ๐ญ๐ข๐จ๐ง๐ฌ ๐ฏ๐ฌ ๐ˆ๐Ž๐ฉ๐ญ๐ข๐จ๐ง๐ฌ๐’๐ง๐š๐ฉ๐ฌ๐ก๐จ๐ญ ๐ฏ๐ฌ ๐ˆ๐Ž๐ฉ๐ญ๐ข๐จ๐ง๐ฌ๐Œ๐จ๐ง๐ข๐ญ๐จ๐ซ ๐ข๐ง .๐๐„๐“ โ€” ๐–๐ก๐š๐ญโ€™๐ฌ ๐ญ๐ก๐ž ๐ƒ๐ข๐Ÿ๐Ÿ๐ž๐ซ๐ž๐ง๐œ๐ž?
Shirin Monzavi
Shirin Monzavi

Posted on

๐Ÿš€ ๐ˆ๐Ž๐ฉ๐ญ๐ข๐จ๐ง๐ฌ ๐ฏ๐ฌ ๐ˆ๐Ž๐ฉ๐ญ๐ข๐จ๐ง๐ฌ๐’๐ง๐š๐ฉ๐ฌ๐ก๐จ๐ญ ๐ฏ๐ฌ ๐ˆ๐Ž๐ฉ๐ญ๐ข๐จ๐ง๐ฌ๐Œ๐จ๐ง๐ข๐ญ๐จ๐ซ ๐ข๐ง .๐๐„๐“ โ€” ๐–๐ก๐š๐ญโ€™๐ฌ ๐ญ๐ก๐ž ๐ƒ๐ข๐Ÿ๐Ÿ๐ž๐ซ๐ž๐ง๐œ๐ž?

โ‰๏ธ ๐–๐ก๐š๐ญ ๐ข๐ฌ ๐ญ๐ก๐ž ๐Ž๐ฉ๐ญ๐ข๐จ๐ง๐ฌ ๐ฉ๐š๐ญ๐ญ๐ž๐ซ๐ง?
You can represent related settings using classes, which gives you strong typing for those settings.

โœจ Benefits:
By applying this pattern, your app follows two important software engineering principles:

๐Ÿ”’ ๐„๐ง๐œ๐š๐ฉ๐ฌ๐ฎ๐ฅ๐š๐ญ๐ข๐จ๐ง โ€“ services only depend on the settings they actually use.
๐Ÿงฉ ๐’๐ž๐ฉ๐š๐ซ๐š๐ญ๐ข๐จ๐ง ๐จ๐Ÿ ๐œ๐จ๐ง๐œ๐ž๐ซ๐ง๐ฌ โ€“ configuration logic is separated from business logic.

โšก ๐‘ถ๐’‘๐’•๐’Š๐’๐’๐’” ๐‘ฐ๐’๐’•๐’†๐’“๐’‡๐’‚๐’„๐’†๐’” ๐’Š๐’ .๐‘ต๐‘ฌ๐‘ป

๐ˆ๐Ž๐ฉ๐ญ๐ข๐จ๐ง๐ฌ
โ€ข Reads configuration once at startup.
โ€ข โŒ Doesnโ€™t update if config changes while the app is running.
โ€ข ๐ŸŸข Registered as Singleton, can be injected anywhere.

๐ˆ๐Ž๐ฉ๐ญ๐ข๐จ๐ง๐ฌ๐’๐ง๐š๐ฉ๐ฌ๐ก๐จ๐ญ
โ€ข Reads configuration fresh on every request.
โ€ข Useful for per-request updates (e.g., in web apps).
โ€ข ๐ŸŸก Registered as Scoped, so cannot be injected into Singletons.

๐ˆ๐Ž๐ฉ๐ญ๐ข๐จ๐ง๐ฌ๐Œ๐จ๐ง๐ข๐ญ๐จ๐ซ
โ€ข Supports real-time updates without restarting the app.
โ€ข Can trigger callbacks when config changes.
โ€ข ๐ŸŸข Registered as Singleton, safe to inject anywhere.

๐Ÿ’ก** Quick Summary**
โ€ข IOptions โ†’ Static, simple, app startup only.
โ€ข IOptionsSnapshot โ†’ Per-request updates.
โ€ข IOptionsMonitor โ†’ Real-time updates with change notifications.

โ“ Which one have you used the most in your projects?

DotNet #DotNetCore #AspNetCore #DotNetDeveloper #CSharp #DotNetTips

Top comments (0)