AspNetCore-Options Pattern

Search for a command to run...

No comments yet. Be the first to comment.
Modern software systems often separate control logic from high-performance execution logic. This design is common in networking, distributed systems, operating systems, storage engines, and embedded s

Introduction Engineering software often combines multiple programming languages to leverage their individual strengths. A common approach is to implement computational algorithms in native C or C++ wh

Deadlocks are one of the most common synchronization problems encountered in operating systems and concurrent programming. Although the concept is frequently introduced in textbooks, observing it insi

Memory and resource allocation are fundamental operations inside the Linux kernel. Whether assigning device IDs, managing CPU masks, allocating interrupt vectors, or tracking hardware resources, the k

The Linux scheduler is one of the most important components of the operating system. Every running program, background service, and kernel thread eventually interacts with the scheduler. In this artic

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.
You are going to create app using Option Pattern
Ubuntu-20.04 +jit [linux-x86_64]
.NET Core- 6.0.16
.NET SDK- 6.0.408
Sqlite- 3.31.1(Optional)
Beginner
Knowledge of C# and text editor/visual studio code
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";
}
}
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);
}
}
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 http://localhost:5000/location to test the new middleware.
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:- https://github.com/aj333git/Plateformus