{"id":25522252,"url":"https://github.com/dotnet-labs/optionspattern","last_synced_at":"2025-10-10T14:33:54.911Z","repository":{"id":108781410,"uuid":"278250749","full_name":"dotnet-labs/OptionsPattern","owner":"dotnet-labs","description":"Options Pattern in .NET Core","archived":false,"fork":false,"pushed_at":"2023-12-07T23:07:18.000Z","size":14,"stargazers_count":9,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-11T01:39:49.325Z","etag":null,"topics":["dependency-injection","design-patterns","dotnet","dotnetcore","options","options-pattern"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dotnet-labs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":"changhuixu","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2020-07-09T03:14:59.000Z","updated_at":"2023-12-07T20:11:58.000Z","dependencies_parsed_at":"2025-04-11T01:34:32.977Z","dependency_job_id":"7032bcbc-ac68-4876-bdfc-0f675293b159","html_url":"https://github.com/dotnet-labs/OptionsPattern","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dotnet-labs/OptionsPattern","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnet-labs%2FOptionsPattern","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnet-labs%2FOptionsPattern/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnet-labs%2FOptionsPattern/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnet-labs%2FOptionsPattern/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dotnet-labs","download_url":"https://codeload.github.com/dotnet-labs/OptionsPattern/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnet-labs%2FOptionsPattern/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279004174,"owners_count":26083688,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection","design-patterns","dotnet","dotnetcore","options","options-pattern"],"created_at":"2025-02-19T18:18:56.427Z","updated_at":"2025-10-10T14:33:54.905Z","avatar_url":"https://github.com/dotnet-labs.png","language":"C#","readme":"# Options Pattern in .NET Core\n\n\u003ca href='https://ko-fi.com/changhuixu' target='_blank'\u003e\u003cimg height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi3.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /\u003e\u003c/a\u003e\n\nThis solution demos one of the usages of the Options Pattern in .NET Core.\n\n## [Medium Article: Options Patter in .NET Core](https://codeburst.io/options-pattern-in-net-core-a50285aeb18d)\n\nWhen registering dependencies in the `ConfigureServices` method, you must have seen a pattern likes the following\n\n```CSharp\nservices.AddDbContext\u003cT\u003e(options =\u003e options.**)\n\nservices.AddSwaggerGen(c =\u003e {\n    c.SwaggerDoc(**);\n})\n\n```\n\nThis pattern is actually an extension method on top of `IServiceCollection`, and the naming convention of this type of extension method is `AddSomeService(this IServiceCollection services, Action\u003cSomeOptions\u003e action)`. The `action` is a Lambda function, which can be used to provide extra parameters to the service.\n\nIn this blog post, we will create a similar service that can be registered by calling the `services.AddMyService(Action\u003cMyServiceOptions\u003e action)` method. We will pass options to `MyService` so that this service can be more flexible with extra parameters.\n\n## Intro to Options pattern in ASP.NET Core\n\n[Microsoft Doc](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options)\n\nThe options pattern uses classes to represent groups of related settings. When configuration settings are isolated by scenario into separate classes, the app adheres to two important software engineering principles:\n\n- The Interface Segregation Principle (ISP) or Encapsulation – Scenarios (classes) that depend on configuration settings depend only on the configuration settings that they use.\n- Separation of Concerns – Settings for different parts of the app aren't dependent or coupled to one another.\n\n## How to Create an Option Object in Tests\n\n```csharp\nOptions.Create(new MyServiceOptions\n                {\n                    Option1 = \"say hello\",\n                    Option2 = true\n                }\n            )\n```\n\n## License\n\nFeel free to use the code in this repository as it is under MIT license.\n\n\u003ca href='https://ko-fi.com/changhuixu' target='_blank'\u003e\u003cimg height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi3.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /\u003e\u003c/a\u003e\n","funding_links":["https://ko-fi.com/changhuixu","https://ko-fi.com/changhuixu'"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdotnet-labs%2Foptionspattern","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdotnet-labs%2Foptionspattern","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdotnet-labs%2Foptionspattern/lists"}