Shared C# library providing common utilities, response builders, and helpers for Minisource .NET microservices.
# Via NuGet (when published)
dotnet add package Minisource.Common
# Via project reference
dotnet add reference ../csharp-common/src/Minisource.CommonMinisource.Common/
├── Auth/ # Authentication helpers
├── Domain/ # Base domain entities
├── Exceptions/ # Custom exceptions
├── Locking/ # Distributed locking
└── Response/ # API response builders
using Minisource.Common.Response;
// Success response
return ResponseBuilder.Success(data, "User created successfully");
// Error response
return ResponseBuilder.Error("Invalid input", 400);
// Paginated response
return ResponseBuilder.Paginated(items, page, pageSize, totalCount);using Minisource.Common.Domain;
public class User : BaseEntity
{
public string Email { get; set; }
public string Name { get; set; }
}
// Inherits: Id, CreatedAt, UpdatedAt, TenantIdusing Minisource.Common.Domain;
public class Payment : TenantEntity
{
public decimal Amount { get; set; }
public string Currency { get; set; }
}
// Includes TenantId property and validationusing Minisource.Common.Exceptions;
// Not found
throw new NotFoundException("User", userId);
// Validation
throw new ValidationException("Email is required");
// Business rule
throw new BusinessException("Insufficient balance");
// Unauthorized
throw new UnauthorizedException("Invalid token");using Minisource.Common.Locking;
var lockManager = new RedisLockManager(redisConnection);
await using (var lockHandle = await lockManager.AcquireAsync("payment:123", TimeSpan.FromSeconds(30)))
{
if (lockHandle != null)
{
// Critical section
await ProcessPayment();
}
}using Minisource.Common.Auth;
// Get tenant from context
var tenantId = HttpContext.GetTenantId();
// Get user claims
var userId = HttpContext.GetUserId();
var roles = HttpContext.GetUserRoles();
// Validate service token
var isValid = await authHelper.ValidateServiceToken(token);{
"Minisource": {
"Auth": {
"ServiceUrl": "http://auth:9001",
"ClientId": "my-service",
"ClientSecret": "secret"
},
"Redis": {
"ConnectionString": "localhost:6379"
}
}
}public void ConfigureServices(IServiceCollection services)
{
// Add common services
services.AddMinisourceCommon(Configuration);
// Add auth
services.AddMinisourceAuth(Configuration);
// Add locking
services.AddDistributedLocking(Configuration);
}app.UseMinisourceExceptionHandler();
// Automatically converts exceptions to proper API responsesapp.UseMultiTenant();
// Extracts and validates tenant from X-Tenant-ID headerAll responses follow a consistent format:
{
"success": true,
"message": "Operation successful",
"data": { ... },
"errors": null,
"meta": {
"timestamp": "2026-02-05T10:30:00Z",
"requestId": "abc-123"
}
}{
"success": true,
"data": [ ... ],
"meta": {
"page": 1,
"pageSize": 20,
"totalCount": 100,
"totalPages": 5,
"hasNext": true,
"hasPrevious": false
}
}# Build
dotnet build
# Test
dotnet test
# Pack for NuGet
dotnet pack -c Release- .NET 10
- Microsoft.Extensions.DependencyInjection
- StackExchange.Redis (for locking)
MIT