Intro:-
The options pattern in ASPDOTNET Core is a technique used to manage and configure settings for your application in a strongly-typed manner. Instead of using app settings files or configuration files directly, you define classes to represent groups of related settings. These classes are then registered with the dependency injection container.
Task:-
You are going to create app using Option Pattern
Software Requirements:-
Ubuntu-20.04 +jit [linux-x86_64]
.NET Core- 6.0.16
.NET SDK- 6.0.408
Sqlite- 3.31.1(Optional)
Level:-
Beginner
Prerequisite:-
Knowledge of C# and text editor/visual studio code
Configuring Middleware:-
There is a common pattern for configuring middleware that is known as the options pattern and that is used by some of the built-in middleware components . The starting point is to define a class that contains the configuration options for a middleware component. Add a class file named GymOptions.cs in the Platformus folder.
namespace Plateformus {
public class GymOptions {
//define properties
public string Machine_Name { get; set; } = "Bench-Press";
public string Category_Name { get; set; } = "Chest_Muscle";
}
}
Options Pattern with Class-Based Middleware:-
Although the options pattern can be used with lambda function middleware, It is better to apply on class-based middleware, When used with class-based middleware, the configuration options don’t have to be accessed through a Program.cs file's Configure method parameter.
public class GymMiddleware {
private RequestDelegate next;
private GymOptions options;
public GymMiddleware(RequestDelegate nextDelegate,
IOptions<GymOptions> opts) {
next = nextDelegate;
options = opts.Value;
}
public async Task Invoke(HttpContext context) {
if (context.Request.Path == "/gym") {
await context.Response
.WriteAsync($"{options.Machine_Name}, {options.Category_Name}");
} else {
await next(context);
}
}
Add Middleware and Options in Program.cs file:-
using Plateformus;
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<GymOptions>(options => {
options.Machine_Name = "Shoulder_Press";
});
var app = builder.Build();
app.UseMiddleware<GymMiddleware>();
app.MapGet("/", () => "Hello World!");
app.Run();
When the UseMiddleware statement is executed, the GymMiddleware constructor is inspected, and its IOptions parameter will be resolved using the object created in the ConfigureServices method. This is done using the dependency injection feature and the the options pattern can be used to easily configure class-based middleware. Restart ASPDOTNET Core and request localhost:5000/location to test the new middleware.
Conclusion:-
The options pattern improves code maintainability and type safety compared to using traditional configuration files. It's particularly useful for managing settings like database connections, API keys, or any other configuration data.
Create Options Class:- Define classes to represent your configuration settings. These classes should have properties that correspond to the settings you want to configure.
Create Middleware Class:- Use IOptions
Interface and dependency injection
, to inject GymOptions
in Middleware class constructor.
Add Middleware and Use DI services to configure options in Program.cs file.
full source code is at:- github.com/aj333git/Plateformus