Simple SDK for ASP.NET Core with built-in support for MediatR, FluentValidation, JWT authentication, RabbitMQ, and Swagger configuration.
Getting Started • Features • Documentation • Contributing
- 🎯 MediatR Integration - Request/response pipeline with validation and logging behaviors
- ✅ FluentValidation - Built-in request validation pipeline behavior
- 🔐 JWT Authentication - Ready-to-use JWT token handling and policies
- 🐇 RabbitMQ - Message broker integration for microservices communication
- 📚 Swagger/OpenAPI - Pre-configured Swagger setup with versioning support
⚠️ Exception Handling - Centralized exception middleware with consistent error responses- 🏗️ Entity Framework Core - Common patterns and helpers for EF Core
- 🎲 Result Pattern - Functional error handling without exceptions
- 🛡️ Guard Clauses - Defensive programming helpers
- 📦 Value Objects - Email, Phone, Money, Address, DateRange
- 🧩 Domain Events - Event-driven architecture support
- 🔄 Specification Pattern - Encapsulated query logic
You should install snglrtycrvtureofspce.Core with NuGet:
Install-Package snglrtycrvtureofspce.CoreOr via the .NET CLI:
dotnet add package snglrtycrvtureofspce.CoreTo reference only the contracts (interfaces) without the full implementation:
Install-Package snglrtycrvtureofspce.Core.ContractsThis package is useful when:
- You need interfaces in a separate assembly
- Building shared contract libraries
- Reducing dependencies in client projects
| Framework | Version |
|---|---|
| .NET | 6.0, 8.0 |
| .NET Standard | 2.0 (Contracts only) |
| .NET Framework | 4.6.2+ (Windows only) |
Register all core services in one line:
// In Program.cs
builder.Services.AddCore(typeof(Program).Assembly);
// Add middleware
app.UseCoreMiddlewares();Add centralized exception handling to your application:
// In Program.cs or Startup.cs
app.UseMiddleware<ExceptionHandlingMiddleware>();The middleware automatically handles:
ValidationException→ HTTP 400NotFoundException→ HTTP 404UnauthorizedAccessException→ HTTP 401ForbiddenAccessException→ HTTP 403ConflictException→ HTTP 409TimeoutException→ HTTP 408
Register the validation behavior:
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>));Create validators for your requests:
public class CreateUserCommandValidator : AbstractValidator<CreateUserCommand>
{
public CreateUserCommandValidator()
{
RuleFor(x => x.Email).NotEmpty().EmailAddress();
RuleFor(x => x.Name).NotEmpty().MaximumLength(100);
}
}Use the built-in exceptions for consistent error handling:
// Not Found with factory method
throw NotFoundException.For<User>(userId);
// Conflict with field info
throw ConflictException.ForField<User>("Email", email);
// Forbidden with role check
throw ForbiddenAccessException.ForRole("Admin");
// Bad Request
throw new BadRequestException("INVALID_INPUT", "Invalid input data");Use Result for functional error handling:
public Result<User> GetUser(int id)
{
var user = _repository.Find(id);
if (user is null)
return Result<User>.Failure(Error.NotFound("User.NotFound", $"User {id} not found"));
return Result<User>.Success(user);
}
// Usage with pattern matching
var result = GetUser(1);
return result.Match(
onSuccess: user => Ok(user),
onFailure: error => NotFound(error.Message)
);
// Chaining operations
var result = GetUser(1)
.Map(user => user.Email)
.Bind(email => ValidateEmail(email))
.Tap(email => _logger.Log($"Valid email: {email}"));Defensive programming made easy:
public void CreateUser(string name, string email, int age)
{
Guard.AgainstNullOrEmpty(name, nameof(name));
Guard.AgainstInvalidEmail(email, nameof(email));
Guard.AgainstNegative(age, nameof(age));
Guard.AgainstOutOfRange(age, 0, 150, nameof(age));
// Continue with valid data...
}Use built-in value objects for common types:
// Email
var email = Email.Create("[email protected]");
// Phone Number
var phone = PhoneNumber.Create("+1 (555) 123-4567");
// Money with currency
var price = Money.Create(99.99m, "USD");
var total = price * 3;
// Address
var address = Address.Create(
street: "123 Main St",
city: "New York",
postalCode: "10001",
country: "USA",
state: "NY"
);
// Date Range
var range = DateRange.Create(DateTime.Today, DateTime.Today.AddDays(7));
if (range.Contains(DateTime.Now)) { /* ... */ }Use base classes for your domain entities:
// Simple entity with Guid ID
public class User : Entity
{
public string Name { get; set; }
}
// Auditable entity with timestamps
public class Order : AuditableEntity
{
public decimal Total { get; set; }
// CreatedAt, CreatedBy, ModifiedAt, ModifiedBy included
}
// Soft-deletable entity
public class Product : SoftDeletableEntity
{
public string Name { get; set; }
// IsDeleted, DeletedAt, DeletedBy included
// Use Delete() and Restore() methods
}Encapsulate query logic:
public class ActiveUsersSpec : Specification<User>
{
public ActiveUsersSpec()
{
AddCriteria(u => u.IsActive);
AddInclude(u => u.Orders);
ApplyOrderBy(u => u.LastLoginDate);
}
}
// Usage
var spec = new ActiveUsersSpec();
var users = await _repository.FindAsync(spec);Rich set of utility extensions:
// Collections
users.IsNullOrEmpty();
items.Batch(100); // Split into chunks
list.ForEach(x => Process(x));
// Strings
"HelloWorld".ToSnakeCase(); // "hello_world"
"[email protected]".Mask(3, 4); // "sec*********\.com"
text.Truncate(50);
// DateTime
date.StartOfMonth();
date.IsWeekend();
date.AddBusinessDays(5);
date.ToRelativeTime(); // "2 hours ago"Dynamic sorting support for LINQ queries:
var sortExpression = ExpressionHelpers.GetSortLambda<User>("LastName");
var sortedUsers = users.AsQueryable().OrderBy(sortExpression);Configure API versioning with Swagger support:
services.AddApiVersioningConfiguration();
services.AddSwaggerConfiguration();- .NET 6.0 SDK or later
- .NET 8.0 SDK (recommended)
# Restore and build
./Build.ps1
# Build contracts only
./BuildContracts.ps1
# Push to NuGet (requires API key)
./Push.ps1 -ApiKey "your-api-key"snglrtycrvtureofspce.Core/
├── src/
│ ├── snglrtycrvtureofspce.Core/ # Main library
│ │ ├── Base/ # Base types and responses
│ │ ├── Enums/ # Common enumerations
│ │ ├── Errors/ # Error handling utilities
│ │ ├── Exceptions/ # Custom exceptions
│ │ ├── Extensions/ # Extension methods
│ │ ├── Filters/ # MVC filters and behaviors
│ │ ├── Helpers/ # Utility helpers
│ │ ├── Microservices/ # Microservices infrastructure
│ │ ├── Middlewares/ # ASP.NET Core middlewares
│ │ └── Providers/ # Test providers
│ └── snglrtycrvtureofspce.Core.Contracts/ # Interfaces and contracts
├── test/
│ └── snglrtycrvtureofspce.Core.Tests/ # Unit tests
├── samples/ # Usage examples
└── assets/ # Logo and assets
We welcome contributions! Please see our Contributing Guidelines for details.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Built with:
- MediatR - Simple mediator implementation
- FluentValidation - Validation library
- Swashbuckle - Swagger/OpenAPI tooling