Skip to content
_viwe edited this page Apr 23, 2023 · 4 revisions

Welcome to the Distro wiki!

gitViwe.Shared

dotnet add package gitViwe.Shared --version 1.4.4

Abstraction:

Defines the schema for the custom ProblemDetails class

interface IDefaultProblemDetails

Exception:

Some custom exceptions

class ForbiddenException { }
class NotFoundException { }
class UnauthorizedException { }
class ServiceUnavailableException { }
class ValidationException { }

Utility:

Some helper classes

static class Conversion {
    static string ByteArrayToString(byte[] value);
    static byte[] StringToByteArray(string hex);
    static string RandomString(int length);
    static DateTime UnixTimeStampToDateTime(long unixTimeStamp);
    static long DateTimeToUnixTimeStamp(DateTime dateTime);
    static byte[] ParseBase64WithoutPadding(string payload);
}

static class Formatter {
    static string FormatSize(long bytes)
}

static class Generator {
    static string RandomString(CharacterCombination combination, int length)
}

Wrapper:

A unified return type for the API endpoint.

Response {
    static Response Fail(string message);
    static Response Fail(string message, int statusCode);
    static Response Success(string message);
    static Response Success(string message, int statusCode);
}

Response<TData> {
    static Response<TData> Fail(string message);
    static Response<TData> Fail(string message, int statusCode);
    static Response<TData> Success(string message, TData data);
    static Response<TData> Success(string message, int statusCode, TData data);
}

PaginatedResponse<TData> {
    static PaginatedResponse<TData> Success(IEnumerable<TData> data, int count, int page, int pageSize);
    static PaginatedResponse<TData> Success(IEnumerable<TData> dataToPaginate, int page, int pageSize);
}

Caching

Nuget package:

dotnet add package gitViwe.Shared.Cache --version 1.4.4

Redis distributed cache:

Register the IRedisDistributedCache service using settings from configuration file

builder.Services.AddGitViweRedisCache(builder.Configuration)

Add configuration to appsettings.json file

"ConnectionStrings": {
    "Redis": "localhost:6379"
  },
  "RedisDistributedCacheOption": {
    "InstanceName": "redis_demo",
    "AbsoluteExpirationInMinutes": 5,
    "SlidingExpirationInMinutes": 2
  }

Register the IRedisDistributedCache service using by specifying the settings values

builder.Services.AddGitViweRedisCache(options =>
{
    options.Configuration = "localhost:6379";
    options.InstanceName= "redis_demo";
    options.AbsoluteExpirationInMinutes = 5;
    options.SlidingExpirationInMinutes = 2;
});

Usage:

public IActionResult Result([FromServices] IRedisDistributedCache redis, [FromBody] UrlShortenRequest request)
{
    await redis.SetAsync(key: "mykey", value: request.Uri, absoluteExpirationRelativeToNow: TimeSpan.FromMinutes(request.MinutesUntilExpiry));

    string value = await redis.GetAsync(key: "mykey") ?? string.Empty;

    return value;
}

Problem Details

Nuget package:

dotnet add package gitViwe.Shared.ProblemDetail --version 1.4.4

Usage:

var extensionValue = new
{
    Balance = 30.0m,
    Accounts = { "/account/12345", "/account/67890" }
};

var problem = ProblemDetailFactory.CreateProblemDetails(
                context: HttpContext,
                statusCode: StatusCodes.Status412PreconditionFailed,
                extensions: new Dictionary<string, object?>()
                {
                    { "outOfCredit", extensionValue }
                },
                detail: "Your current balance is 30, but that costs 50.");