Skip to content

minisource/csharp-common

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Minisource C# Common Library

Shared C# library providing common utilities, response builders, and helpers for Minisource .NET microservices.

Installation

# Via NuGet (when published)
dotnet add package Minisource.Common

# Via project reference
dotnet add reference ../csharp-common/src/Minisource.Common

Package Structure

Minisource.Common/
├── Auth/           # Authentication helpers
├── Domain/         # Base domain entities
├── Exceptions/     # Custom exceptions
├── Locking/        # Distributed locking
└── Response/       # API response builders

Quick Start

Response Builder

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);

Base Entity

using Minisource.Common.Domain;

public class User : BaseEntity
{
    public string Email { get; set; }
    public string Name { get; set; }
}
// Inherits: Id, CreatedAt, UpdatedAt, TenantId

Multi-Tenant Entity

using Minisource.Common.Domain;

public class Payment : TenantEntity
{
    public decimal Amount { get; set; }
    public string Currency { get; set; }
}
// Includes TenantId property and validation

Custom Exceptions

using 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");

Distributed Locking

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();
    }
}

Auth Helpers

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);

Configuration

appsettings.json

{
  "Minisource": {
    "Auth": {
      "ServiceUrl": "http://auth:9001",
      "ClientId": "my-service",
      "ClientSecret": "secret"
    },
    "Redis": {
      "ConnectionString": "localhost:6379"
    }
  }
}

Dependency Injection

public void ConfigureServices(IServiceCollection services)
{
    // Add common services
    services.AddMinisourceCommon(Configuration);
    
    // Add auth
    services.AddMinisourceAuth(Configuration);
    
    // Add locking
    services.AddDistributedLocking(Configuration);
}

Middleware

Exception Handler

app.UseMinisourceExceptionHandler();
// Automatically converts exceptions to proper API responses

Tenant Middleware

app.UseMultiTenant();
// Extracts and validates tenant from X-Tenant-ID header

Response Format

All responses follow a consistent format:

{
  "success": true,
  "message": "Operation successful",
  "data": { ... },
  "errors": null,
  "meta": {
    "timestamp": "2026-02-05T10:30:00Z",
    "requestId": "abc-123"
  }
}

Paginated Response

{
  "success": true,
  "data": [ ... ],
  "meta": {
    "page": 1,
    "pageSize": 20,
    "totalCount": 100,
    "totalPages": 5,
    "hasNext": true,
    "hasPrevious": false
  }
}

Building

# Build
dotnet build

# Test
dotnet test

# Pack for NuGet
dotnet pack -c Release

Dependencies

  • .NET 10
  • Microsoft.Extensions.DependencyInjection
  • StackExchange.Redis (for locking)

License

MIT

About

Shared C# libraries, helpers and utilities

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages