Git Product home page Git Product logo

optionspattern's Introduction

Options Pattern in .NET Core

Buy Me a Coffee at ko-fi.com

This solution demos one of the usages of the Options Pattern in .NET Core.

When registering dependencies in the ConfigureServices method, you must have seen a pattern likes the following

services.AddDbContext<T>(options => options.**)

services.AddSwaggerGen(c => {
    c.SwaggerDoc(**);
})

This 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<SomeOptions> action). The action is a Lambda function, which can be used to provide extra parameters to the service.

In this blog post, we will create a similar service that can be registered by calling the services.AddMyService(Action<MyServiceOptions> action) method. We will pass options to MyService so that this service can be more flexible with extra parameters.

Intro to Options pattern in ASP.NET Core

Microsoft Doc

The 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:

  • The Interface Segregation Principle (ISP) or Encapsulation โ€“ Scenarios (classes) that depend on configuration settings depend only on the configuration settings that they use.
  • Separation of Concerns โ€“ Settings for different parts of the app aren't dependent or coupled to one another.

How to Create an Option Object in Tests

Options.Create(new MyServiceOptions
                {
                    Option1 = "say hello",
                    Option2 = true
                }
            )

License

Feel free to use the code in this repository as it is under MIT license.

Buy Me a Coffee at ko-fi.com

optionspattern's People

Contributors

changhuixu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.