SliceR 1.0.0

dotnet add package SliceR --version 1.0.0
                    
NuGet\Install-Package SliceR -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SliceR" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SliceR" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SliceR" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SliceR --version 1.0.0
                    
#r "nuget: SliceR, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=SliceR&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SliceR&version=1.0.0
                    
Install as a Cake Tool

SliceR

SliceR is a lightweight library that integrates MediatR with FluentValidation and ASP.NET Core Authorization to enable clean, vertical "slicing" of application features. This approach helps maintain separation of concerns while ensuring proper validation and authorization across your application.

Features

  • MediatR Integration: Seamlessly works with MediatR for command and query handling
  • Automated Validation: Integrates FluentValidation to validate commands and queries before they're handled
  • Policy-Based Authorization: Enforces authorization policies on requests
  • Resource-Based Authorization: Supports authorization against specific resources
  • ASP.NET Core Integration: Provides middleware and exception filters for web applications
  • Minimal Configuration: Set up with a single extension method

Installation

Add a reference to the SliceR project in your solution:

<ProjectReference Include="..\path\to\SliceR\src\SliceR\SliceR.csproj" />

Getting Started

1. Register Services

In your Program.cs or Startup.cs:

using SliceR;

// ...

services.AddSliceR(typeof(YourStartupClass).Assembly);

This will register all MediatR handlers, validators, and authorization components.

2. Create Authorized Requests

For requests requiring authentication only:

public record GetUserDataQuery : IAuthorizedRequest<UserDataResponse>
{
    public string? UserId { get; init; }
    
    // No specific policy, just authentication
    public string? PolicyName => null;
}

For requests requiring specific authorization policies:

public record DeleteUserCommand : IAuthorizedRequest<bool>
{
    public string UserId { get; init; }
    
    // Require the "users.delete" policy
    public string PolicyName => "users.delete";
}

3. Resource-Based Authorization

For operations on specific resources:

public record UpdateDocumentCommand : IAuthorizedResourceRequest<Document, Unit>
{
    public string DocumentId { get; init; }
    public string NewContent { get; init; }
    
    // The resource being accessed
    public Document Resource { get; init; }
    
    // The policy to check
    public string PolicyName => "documents.update";
}

4. Adding Validation

Create validators using FluentValidation:

public class CreateUserCommandValidator : AbstractValidator<CreateUserCommand>
{
    public CreateUserCommandValidator()
    {
        RuleFor(x => x.Username).NotEmpty().MinimumLength(3).MaximumLength(50);
        RuleFor(x => x.Email).NotEmpty().EmailAddress();
        RuleFor(x => x.Password).NotEmpty().MinimumLength(8);
    }
}

How It Works

SliceR adds pipeline behaviors to MediatR:

  1. Validation Behavior: Automatically validates requests using registered FluentValidation validators
  2. Authorization Behavior: Ensures users are authorized to perform the requested operation

When a request fails validation or authorization, appropriate exceptions are thrown and handled by the registered exception filters.

Advanced Configuration

Custom Authorization Provider

You can implement your own authorization provider:

public class CustomAuthorizationProvider : IAuthorizationProvider
{
    // Implement authorization logic
    public Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, string policyName)
    {
        // Custom authorization logic
    }
    
    public Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, string policyName, object resource)
    {
        // Custom resource-based authorization logic
    }
}

// Register your custom provider
services.AddScoped<IAuthorizationProvider, CustomAuthorizationProvider>();

Service Lifetime

You can customize the service lifetime when registering SliceR:

services.AddSliceR(
    assembly: typeof(YourStartupClass).Assembly,
    lifetime: ServiceLifetime.Singleton,
    includeInternalTypes: true
);

Benefits of the Vertical Slice Architecture

Using SliceR helps implement a vertical slice architecture where:

  • Each feature is isolated with its own request, handler, validator, and authorization rules
  • Cross-cutting concerns like validation and authorization are handled consistently
  • Code organization follows feature boundaries rather than technical layers
  • Features can be understood, tested, and maintained in isolation
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 277 6/12/2025

Initial release of SliceR library.