eBuildingBlocks.Application
2.0.5
dotnet add package eBuildingBlocks.Application --version 2.0.5
NuGet\Install-Package eBuildingBlocks.Application -Version 2.0.5
<PackageReference Include="eBuildingBlocks.Application" Version="2.0.5" />
<PackageVersion Include="eBuildingBlocks.Application" Version="2.0.5" />
<PackageReference Include="eBuildingBlocks.Application" />
paket add eBuildingBlocks.Application --version 2.0.5
#r "nuget: eBuildingBlocks.Application, 2.0.5"
#:package eBuildingBlocks.Application@2.0.5
#addin nuget:?package=eBuildingBlocks.Application&version=2.0.5
#tool nuget:?package=eBuildingBlocks.Application&version=2.0.5
eBuildingBlocks.Application
A comprehensive application layer building block for .NET applications that provides essential cross-cutting concerns, exception handling, response models, and application utilities.
Overview
eBuildingBlocks.Application is a foundational library that encapsulates common application patterns, exception handling, response models, and middleware components. It provides a standardized approach to building application layers with built-in support for validation, exception handling, and response formatting.
Key Features
🛡️ Exception Handling
- Global Exception Handler: Centralized exception handling middleware
- Custom Exceptions: Predefined exception types for common scenarios
- HTTP Response Middleware: Standardized HTTP response formatting
- Error Response Models: Consistent error response structures
📋 Response Models
- Standardized Response Format: Consistent API response structure
- Paged List Support: Built-in pagination support
- View Models: Common view model patterns
- Enum View Models: Enumeration display models
🔧 Middleware Components
- HTTP Response Middleware: Response formatting and logging
- Global Exception Handler: Centralized error handling
- Request/Response Logging: Built-in request/response logging
✅ Validation & Business Logic
- FluentValidation Integration: Built-in validation support
- AutoMapper Integration: Object mapping utilities
- Feature Management: Feature flag support
Installation
dotnet add package eBuildingBlocks.Application
Quick Start
1. Register Services
using eBuildingBlocks.Application;
var builder = WebApplication.CreateBuilder(args);
// Register application services
builder.Services.AddApplication();
2. Configure Middleware
var app = builder.Build();
// Add global exception handler
app.UseMiddleware<GlobalExceptionHandler>();
// Add HTTP response middleware
app.UseMiddleware<HttpResponseMiddleware>();
3. Use Response Models
using eBuildingBlocks.Application.Models;
public class UserController : ControllerBase
{
[HttpGet]
public async Task<ResponseModel<List<UserDto>>> GetUsers()
{
var users = await _userService.GetAllAsync();
return ResponseModel<List<UserDto>>.Success(users);
}
}
Features in Detail
Exception Handling
The library provides a comprehensive exception handling system:
// Global exception handler automatically catches and formats exceptions
app.UseMiddleware<GlobalExceptionHandler>();
Available Exception Types
// HTTP 400 - Bad Request
throw new BadRequestException("Invalid input");
// HTTP 401 - Unauthorized
throw new UnauthorizedException("Authentication required");
// HTTP 403 - Forbidden
throw new ForbiddenException("Access denied");
// HTTP 404 - Not Found
throw new NotFoundException("Resource not found");
// HTTP 405 - Method Not Allowed
throw new MethodNotAllowedException("Method not supported");
// HTTP 409 - Conflict
throw new ConflictException("Resource conflict");
// HTTP 429 - Too Many Requests
throw new TooManyRequestException("Rate limit exceeded");
// HTTP 501 - Not Implemented
throw new NotImplementedException("Feature not implemented");
Response Models
Standard Response Format
public class ResponseModel<T>
{
public bool IsSuccess { get; set; }
public string Message { get; set; }
public T Data { get; set; }
public List<string> Errors { get; set; }
public int StatusCode { get; set; }
}
// Usage
return ResponseModel<UserDto>.Success(user);
return ResponseModel<UserDto>.Failure("User not found", 404);
Paged List Support
public class PagedList<T>
{
public List<T> Items { get; set; }
public int PageNumber { get; set; }
public int PageSize { get; set; }
public int TotalCount { get; set; }
public int TotalPages { get; set; }
public bool HasPreviousPage { get; set; }
public bool HasNextPage { get; set; }
}
View Models
Entity View Model
public class EntityViewModel
{
public Guid Id { get; set; }
public string Name { get; set; }
}
Enum View Model
public class EnumViewModel
{
public int Value { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
}
Redirect View Model
public class RedirectViewModel
{
public string Url { get; set; }
public bool IsPermanent { get; set; }
}
Project Structure
eBuildingBlocks.Application/
├── Exceptions/
│ ├── BadRequestException.cs
│ ├── ConflictException.cs
│ ├── ForbiddenException.cs
│ ├── MethodNotAllowedException.cs
│ ├── NotFoundException.cs
│ ├── NotImplementedException.cs
│ ├── TooManyRequestException.cs
│ └── UnauthorizedException.cs
├── Models/
│ ├── PagedList.cs
│ └── ResponseModel.cs
├── ViewModels/
│ ├── EntityViewModel.cs
│ ├── EnumViewModel.cs
│ └── RedirectViewModel.cs
├── Middlewares/
│ └── HttpResponseMiddleware.cs
├── GlobalExceptionHandler.cs
└── eBuildingBlocks.Application.csproj
Usage Examples
Custom Exception Handler
public class CustomExceptionHandler : GlobalExceptionHandler
{
protected override async Task HandleExceptionAsync(HttpContext context, Exception exception)
{
// Custom exception handling logic
await base.HandleExceptionAsync(context, exception);
}
}
Custom Response Middleware
public class CustomResponseMiddleware : HttpResponseMiddleware
{
protected override async Task ProcessResponseAsync(HttpContext context)
{
// Custom response processing
await base.ProcessResponseAsync(context);
}
}
Using Response Models
[HttpGet]
public async Task<ResponseModel<PagedList<UserDto>>> GetUsers(int page = 1, int pageSize = 10)
{
try
{
var users = await _userService.GetPagedAsync(page, pageSize);
return ResponseModel<PagedList<UserDto>>.Success(users);
}
catch (Exception ex)
{
return ResponseModel<PagedList<UserDto>>.Failure(ex.Message, 500);
}
}
Custom Exception
public class CustomBusinessException : Exception
{
public CustomBusinessException(string message) : base(message)
{
}
}
// Usage
throw new CustomBusinessException("Business rule violation");
Dependencies
- ASP.NET Core 9.0
- FluentValidation - Input validation
- AutoMapper - Object mapping
- Microsoft.FeatureManagement - Feature flags
- Polly - Resilience and transient-fault-handling
- Swashbuckle.AspNetCore.Annotations - API documentation
- System.IdentityModel.Tokens.Jwt - JWT token handling
Configuration
Exception Handling Configuration
// Configure global exception handler
app.UseMiddleware<GlobalExceptionHandler>();
// Configure response middleware
app.UseMiddleware<HttpResponseMiddleware>();
Validation Configuration
// Register FluentValidation
services.AddFluentValidationAutoValidation();
services.AddValidatorsFromAssemblyContaining<Startup>();
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
Copyright © Inam Ul Haq. All rights reserved.
Support
- Author: Inam Ul Haq
- Email: inam.sys@gmail.com
- LinkedIn: https://www.linkedin.com/in/inam1567/
- Repository: https://github.com/ghazi1567/eBuildingBlocks
| Product | Versions 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. |
-
net9.0
- AutoMapper (>= 14.0.0)
- eBuildingBlocks.Domain (>= 2.0.4)
- FluentValidation (>= 11.11.0)
- FluentValidation.DependencyInjectionExtensions (>= 11.11.0)
- Humanizer.Core (>= 2.14.1)
- MediatR (>= 12.5.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.4)
- Microsoft.Extensions.DependencyInjection (>= 9.0.4)
- Microsoft.FeatureManagement (>= 4.0.0)
- Polly (>= 8.5.2)
- Swashbuckle.AspNetCore.Annotations (>= 8.1.1)
- Swashbuckle.AspNetCore.Filters (>= 8.0.2)
- System.IdentityModel.Tokens.Jwt (>= 8.9.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on eBuildingBlocks.Application:
| Package | Downloads |
|---|---|
|
eBuildingBlocks.API
Reusable building block for the ecosystem. Implements core cross-cutting concerns, infrastructure, or domain utilities for .NET applications. |
GitHub repositories
This package is not used by any popular GitHub repositories.