DependencyInjectionExtensions
Namespace: RA.Utilities.Authorization.Extensions
The DependencyInjectionExtensions class provides a convenient extension method to simplify the registration of the ICurrentUser service in your application's dependency injection container.
🎯 Purpose
The DependencyInjectionExtensions class provides a convenient shortcut for registering the services related to the AppUser utility.
Its purpose is to simplify the setup process in Program.cs.
Instead of requiring developers to know that AppUser depends on IHttpContextAccessor and registering both manually, this class provides a single extension method, AddCurrentUser(), that handles all the necessary registrations.
This approach:
- Reduces Boilerplate: It turns a multi-line setup into a single, declarative call.
- Encapsulates Implementation Details: It hides the fact that
AppUserneedsIHttpContextAccessor, making the setup cleaner and less prone to error. - Promotes Best Practices: By registering the service, it encourages developers to depend on abstractions, not concrete implementations, which is crucial for testability and maintainability.
🧩 Available Extensions
AddCurrentUser()
Registers AppUser as the transient. It also registers IHttpContextAccessor, which is required by AppUser to access the current request's user claims.
Usage
Call AddCurrentUser() in your Program.cs file when configuring your services.
// Program.cs
using RA.Utilities.Authorization.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddCurrentUser();
// ... other service registrations
After registration, you can inject AppUser into your controllers and services to access information about the authenticated user.