Skip to content

⛵ Caravel is a SDK to help developers building .NET applications.

License

Notifications You must be signed in to change notification settings

adrianiftode/Caravel

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Caravel

Caravel is a SDK to help developers building .NET applications.

Features

This project is split in 2 different packages:

Caravel

This package does not have any external dependency and it brings all basic utilities that every application should handle such as application context or exceptions.

  • Application context (UserId, TraceId, TenantId)
//Inject IAppContextAccessor on constructor.
IAppContextAccessor contextAccessor = ...

//Get the application context.
AppContext context = contextAccessor.Context;

//Get UserId of the current user.
context.UserId;

//Get TenantId of the current tenant. It is useful when dealing with multitenant applications.
context.TenantId;

//Get TraceId of the current execution.
context.TraceId;
  • Exceptions and Errors (NotFound, Validation, Permission, etc...)
var DatabaseError = new Error(10001, "Cannot connect with Database", Severity.High);

var exception = new CaravelException(DatabaseError, innerException);
  • DateTime Clock
IClock clock = new DateTimeClock();

var nowUtc = clock.NowUtc();
  • HttpClient Extension Methods
HttpClient client = new HttpClient();

client.PostJsonAsync("api/v1/books", createBook, cancellationToken);
  • Json Converters
var json = JsonSerializer.Serialize(obj, JsonSerializerOptions.CamelCase());
  • Functional

Optional - Avoid Nulls using Optional Types

Optional<Product> result = await service.GetProductAsync(id);

return result switch {
            Some<Product> p => Ok(p),
            _ => NotFound()
        };

Either - Improve your error handling by using Either<Error,Success>

Either.Right<Error, Product> result = await service.GetProductAsync(id);

return result.Fold(
    (e) => new HttpError(e.Message),
    (product) => Ok(product));

Caravel.AspNetCore

This package contains reusable middleware, http utilities that every application needs.

  • ValidateModelFilter
.AddMvcOptions(opt =>
{
    opt.Filters.Add(new ValidateModelFilter());
})
  • Application version Middleware
app.UseAppVersion("/api/version");
  • Logging Middleware
app.UseMiddleware<LoggingMiddleware>(Options.Create(new LoggingOptions
{
    EnableLogBody = true
}));
  • Exception Middleware
// Handle exceptions according to RFC: https://tools.ietf.org/html/rfc7807
app.UseMiddleware<ExceptionMiddleware>();
{
  "traceId": "30b860d8-03cd-440e-9653-d5f0d090d86a",
  "code": 30001,
  "title": "Book does not exist",
  "status": 404,
  "detail": "Book 53655b3d-48d5-4ac1-ba73-4318b3b702e8 does not exist",
  "instance": "/api/v1/books/53655b3d-48d5-4ac1-ba73-4318b3b702e8"
}
  • TraceId/CorrelationId Middleware
app.UseMiddleware<TraceIdMiddleware>();

Projects using Caravel

  • Caravel Template - .NET template that generates a functional web api using the best practices.
  • .NET Careers - Connecting .NET engineers with amazing companies and help them to find their next challenge.

Credits

Logo made by Freepik from www.flaticon.com

About

⛵ Caravel is a SDK to help developers building .NET applications.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%