Siemens.AspNet.MinimalApi.Sdk 0.1.0-alpha.138

Prefix Reserved
This is a prerelease version of Siemens.AspNet.MinimalApi.Sdk.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Siemens.AspNet.MinimalApi.Sdk --version 0.1.0-alpha.138
                    
NuGet\Install-Package Siemens.AspNet.MinimalApi.Sdk -Version 0.1.0-alpha.138
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Siemens.AspNet.MinimalApi.Sdk" Version="0.1.0-alpha.138" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Siemens.AspNet.MinimalApi.Sdk" Version="0.1.0-alpha.138" />
                    
Directory.Packages.props
<PackageReference Include="Siemens.AspNet.MinimalApi.Sdk" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Siemens.AspNet.MinimalApi.Sdk --version 0.1.0-alpha.138
                    
#r "nuget: Siemens.AspNet.MinimalApi.Sdk, 0.1.0-alpha.138"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Siemens.AspNet.MinimalApi.Sdk&version=0.1.0-alpha.138&prerelease
                    
Install Siemens.AspNet.MinimalApi.Sdk as a Cake Addin
#tool nuget:?package=Siemens.AspNet.MinimalApi.Sdk&version=0.1.0-alpha.138&prerelease
                    
Install Siemens.AspNet.MinimalApi.Sdk as a Cake Tool

Siemens.AspNet.Minimal.Sdk

The Siemens.AspNet.Minimal.Sdk NuGet package offers a streamlined approach to building Minimal APIs in ASP.NET Core. It includes pre-configured defaults, opinionated helpers, and seamless integrations that accelerate development and simplify application setup.


📖 Overview

This SDK is designed to remove boilerplate code and help you focus on what matters: your business logic.

✅ Key Features

  • ⚙️ Pre-configured application startup
  • ☁️ AWS integration (optional)
  • 🗄️ Database connectivity (supports AWS DynamoDB)
  • 🔄 Consistent JSON serialization settings
  • 🔐 Security best practices (JWT, OAuth2, security headers)
  • ✔️ Custom validation support
  • 📘 Auto-configured OpenAPI/Swagger

📦 Installation

Using the .NET CLI

dotnet add package Siemens.AspNet.Minimal.Sdk

⚡ Quickstart Example

Below is a minimal setup using ServerlessMinimalWebApi, designed to get you started in seconds:

using Pulse.FieldingTool.Api;
using Siemens.AspNet.MinimalApi.Sdk;

// Create the minimal API host
var webApi = new ServerlessMinimalWebApi();

// Optionally set a global base path for all endpoints
webApi.BasePath = "api/fieldingtool";

// Register application services
webApi.RegisterServices = (services, config) =>
{
    services.AddApi(config);
};

// Map API endpoints
webApi.MapEndpoints = endpoints =>
{
    endpoints.MapApi();
};

// Run the application
webApi.Run(args);

// Enable testing via WebApplicationFactory<Program>
namespace Pulse.FieldingTool
{
    public partial class Program {}
}

🧠 Key Concepts

Component Description
ServerlessMinimalWebApi Opinionated builder for hosting, logging, and config setup
RegisterServices Delegate for adding services to the DI container
MapEndpoints Delegate for defining endpoint mappings using Minimal API routing
Run Starts the web application
partial Program Enables integration testing with WebApplicationFactory<Program>

Helpers and utils

Activator Usage

public class ExampleService
{
    private readonly IActivator _activator;

    public ExampleService(IActivator activator)
    {
        _activator = activator;
    }

    public async Task<MyClass> CreateMyClassAsync()
    {
        return await _activator.CreateInstanceAsync<MyClass>();
    }
}

JSON Differ Example

public void CheckJsonDifferences(IJsonDiffer jsonDiffer)
{
    var differences = jsonDiffer.FindDifferences("{\"name\":\"John\"}", "{\"name\":\"Jane\"}");
    foreach (var diff in differences)
    {
        Console.WriteLine($"Property: {diff.MemberPath}, Difference: {diff.MismatchType}");
    }
}

Custom Validation with Attributes

internal static class AddAlphaNumericValidatorExtension
{
    internal static void AddAlphaNumericValidator(this IServiceCollection services)
    {
        services.AddSingletonIfNotExists<ICustomValidator, AlphaNumericValidator>();
    }
}

public class AlphaNumericAttribute(params string[] sampleValues) : CustomValidationAttribute<AlphaNumericValidator>(string.Empty, sampleValues);

public sealed class AlphaNumericValidator : CustomValidatorBase<string, AlphaNumericAttribute>
{
    protected override IEnumerable<ValidationErrorDetailsBase> Validate(PropertyInfo propertyInfo,
                                                                        AlphaNumericAttribute attribute,
                                                                        string? source)
    {
        if (source == null)
        {
            yield break;
        }

        if (source.Any(ch => !char.IsLetterOrDigit(ch)))
        {
            var sampleValues = attribute.SampleValues.Any() ? attribute.SampleValues : ["sample123"];

            yield return new ValidationErrorDetailsBase
            {
                Errors = [$"{propertyInfo.Name} must be alphanumeric."],
                Samples = sampleValues
            };
        }
    }
}

Request Validator (sync)

This request validator shows a sample without attribute validation.

public static class AddCreateFormsConfigurationRequestValidatorExtension
{
    internal static void AddCreateFormsConfigurationRequestValidator(this IServiceCollection services,
                                                                     IConfiguration configuration)
    {
        services.AddLanguageValidator();
        services.AddFormsConfigurationSettings(configuration);

        services.AddSingletonIfNotExists<CreateFormsConfigurationRequestValidator>();
    }
}

internal sealed class CreateFormsConfigurationRequestValidator(LanguageValidator languageValidator,
                                                               FormsConfigurationSettings formsConfigurationSettings,
                                                               IAttributeValidator attributeValidator) : RequestValidator<CreateFormsConfigurationRequest>(IAttributeValidator attributeValidator)
{
    protected override IEnumerable<PropertyValidationResult> GetValidationErrors(CreateFormsConfigurationRequest request)
    {
        // Your validation code here
        if (request.FormsId.IsNull())
        {
            var errorDetails = new ValidationErrorDetails
                                {
                                    CurrentValue = request.FormsId,
                                    Errors = [$"{nameof(request.FormsId)} must not be null. Only GUID or a long is valid for the {nameof(request.FormsId)}"],
                                    Samples = ["a1b2c3d4-e5f6-7890-1234-567890abcdef", "1"]
                                };

            yield return new PropertyValidationResult(nameof(request.FormsId), errorDetails);
        }
    }
}

Async Request Validator

In this sample the async validator is used to validate a CreateDeploymentRequest object. The validator checks for the presence of required properties and validates them using the IAttributeValidator interface.

public sealed record CreateDeploymentRequest
{
    [ValidEnum]
    public required Stage Stage { get; init; }
};
internal static class AddCreateDeploymentRequestValidatorExtension
{
    internal static void AddCreateDeploymentRequestValidator(this IServiceCollection services)
    {
        services.AddSingletonIfNotExists<CreateDeploymentRequestValidator>();
    }
}

internal sealed class CreateDeploymentRequestValidator(IAttributeValidator attributeValidator) : AsyncRequestValidator<CreateDeploymentRequest>
{
    protected override async IAsyncEnumerable<PropertyValidationResult> GetValidationErrorsAsync(CreateDeploymentRequest request,
                                                                                                    [EnumeratorCancellation] CancellationToken cancellationToken = default)
    {
        var errors = await attributeValidator.ValidateAsync(request, cancellationToken).ConfigureAwait(false);
        foreach (var propertyValidationResult in errors)
        {
            yield return propertyValidationResult;
        }
    }
}

AWS DynamoDB Custom Converters

The Siemens.AspNet.MinimalApi.Sdk already provides out of the box some helpers for the most common types. You can use them directly or implement your own converters.

Converter Name Description
DateTimeOffsetConverter Converts DateTimeOffset values to and from string format (typically ISO 8601) for DynamoDB.
DictionaryStringObjectConverter Handles conversion of Dictionary<string, object> to a DynamoDB-compatible format.
DictionaryStringObjectNullableConverter Similar to DictionaryStringObjectConverter but allows nullable dictionary handling.
ImmutableDictionaryStringObjectNullableConverter Converts ImmutableDictionary<string, object?> to a format compatible with DynamoDB, supporting null values.
TimeSpanConverter Serializes TimeSpan values as string and deserializes them back. Useful for time duration storage in DynamoDB.

Sample:

[DynamoDBTable("Capability")]
public record CapabilityEntity
{
    [DynamoDBHashKey]
    public required Guid Id { get; init; }

    [DynamoDBRangeKey]
    public required string DeploymentId { get; init; } = CapabilityConstants.DefaultDeploymentId;

    [DynamoDBProperty(typeof(DateTimeOffsetConverter))]
    public required DateTimeOffset LastUpdatedDate { get; init; } = DateTimeOffset.UtcNow;
}

AWS Dynamo Entity Mapper (POC)

The IDynamoEntityMapper brings already most common converter with it (Siemens.AspNet.MinimalApi.Sdk). You can just use it. In exception cases, you can implement your own converter by implementing IDynamoTypeConverter or IAsyncDynamoTypeConverter.

Converter Name Description
EnumToStringConverter Converts enum values to their string representation and vice versa. Useful for storing enums as strings in DynamoDB.
EnumerableToImmutableConverter Converts IEnumerable<T> to ImmutableList<T> for safe, immutable handling of collections during mapping.
ImmutableToListConverter Converts ImmutableList<T> to List<T> to support serialization or mutable collection use cases.
TimeSpanToStringConverter Serializes TimeSpan values as ISO 8601-like strings and parses them back. Enables human-readable time span storage.
public sealed class MyHandler(IDynamoEntityMapper mapper)
{
    public async Task<MyDto> HandleAsync(object rawData, CancellationToken cancellationToken)
    {
        return await mapper.ConvertAsync<MyDto>(rawData, cancellationToken);
    }
}

Custom property converter example:

internal static class AddTimeSpanToStringConverterExtension
{
    internal static void AddTimeSpanToStringConverter(this IServiceCollection services)
    {
        services.AddSingletonIfNotExists<IDynamoTypeConverter, TimeSpanToStringConverter>();
    }
}

internal sealed class TimeSpanToStringConverter : IDynamoTypeConverter
{
    public bool CanConvert(Type source,
                            Type target)
    {
        var canConvert = source == typeof(TimeSpan) &&
                            target == typeof(string);

        return canConvert;
    }

    public object? ConvertObject(object? source,
                                    Type targetType)
    {
        return source?.ToString();
    }
}

📌 Usage & Best Practices

  • Leverage IActivator for instance creation with dependency injection.
  • Leverage IAsyncActivator for instance creation with dependency injection in asynchronous contexts.
  • Utilize IJsonDiffer for tracking JSON changes in PATCH requests.
  • Implement custom validation logic by extending provided base validator classes.
  • Implement IAttributeValidator custom for attribute-driven validation scenarios.
  • Simplified attribut-based validation by the IAttributeValidator interface.
  • Use IDynamoTypeConverter or IAsyncDynamoTypeConverter to encapsulate DynamoDB-safe conversions.
  • Register custom converters and provide an implementation of IDynamoEntityMapper to centralize conversion logic.

📚 Documentation

Additional details and examples are available in the repository documentation and upcoming online resources.


📢 Contributing

Your contributions and feedback are welcomed! Please create issues or pull requests to enhance this package.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Siemens.AspNet.MinimalApi.Sdk:

Package Downloads
Siemens.AspNet.MsTest.Sdk

A library which contains following functions: - Siemens.AspNet.MsTest.Sdk

Siemens.AspNet.DbProvider

A library which contains following functions: - Siemens.AspNet.DbProvider

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.0-alpha.178 0 6/23/2025
0.1.0-alpha.176 0 6/23/2025
0.1.0-alpha.174 105 6/19/2025
0.1.0-alpha.173 128 6/19/2025
0.1.0-alpha.172 106 6/17/2025
0.1.0-alpha.171 180 6/16/2025
0.1.0-alpha.169 105 6/16/2025
0.1.0-alpha.165 231 6/13/2025
0.1.0-alpha.164 213 6/13/2025
0.1.0-alpha.163 218 6/13/2025
0.1.0-alpha.160 255 6/12/2025
0.1.0-alpha.159 306 6/11/2025
0.1.0-alpha.158 257 6/11/2025
0.1.0-alpha.143 251 6/11/2025
0.1.0-alpha.142 251 6/11/2025
0.1.0-alpha.140 250 6/11/2025
0.1.0-alpha.139 301 6/10/2025
0.1.0-alpha.138 245 6/9/2025
0.1.0-alpha.137 40 6/7/2025
0.1.0-alpha.136 36 6/7/2025
0.1.0-alpha.135 65 6/6/2025
0.1.0-alpha.134 63 6/6/2025
0.1.0-alpha.130 116 6/5/2025
0.1.0-alpha.129 108 6/4/2025
0.1.0-alpha.128 106 6/4/2025
0.1.0-alpha.122 172 6/3/2025
0.1.0-alpha.121 116 6/1/2025
0.1.0-alpha.120 74 6/1/2025
0.1.0-alpha.118 114 5/28/2025
0.1.0-alpha.117 113 5/28/2025
0.1.0-alpha.116 141 5/28/2025
0.1.0-alpha.115 115 5/26/2025
0.1.0-alpha.114 141 5/22/2025
0.1.0-alpha.112 114 5/21/2025
0.1.0-alpha.111 109 5/20/2025
0.1.0-alpha.108 164 5/19/2025
0.1.0-alpha.104 475 5/18/2025
0.1.0-alpha.102 339 5/14/2025
0.1.0-alpha.101 193 5/14/2025
0.1.0-alpha.100 196 5/12/2025
0.1.0-alpha.99 222 5/12/2025
0.1.0-alpha.98 38 5/10/2025
0.1.0-alpha.97 37 5/10/2025
0.1.0-alpha.86 134 5/8/2025
0.1.0-alpha.85 106 5/8/2025
0.1.0-alpha.84 106 5/8/2025
0.1.0-alpha.82 117 5/7/2025
0.1.0-alpha.81 107 5/6/2025
0.1.0-alpha.76 41 5/3/2025
0.1.0-alpha.75 58 5/2/2025
0.1.0-alpha.74 65 5/2/2025
0.1.0-alpha.56 127 4/28/2025
0.1.0-alpha.55 122 4/28/2025
0.1.0-alpha.54 192 4/14/2025
0.1.0-alpha.53 179 4/14/2025
0.1.0-alpha.48 439 4/14/2025
0.1.0-alpha.47 159 4/9/2025
0.1.0-alpha.46 235 4/9/2025
0.1.0-alpha.44 122 4/7/2025
0.1.0-alpha.41 118 4/7/2025
0.1.0-alpha.40 126 4/7/2025
0.1.0-alpha.39 82 4/4/2025
0.1.0-alpha.38 87 4/4/2025
0.1.0-alpha.37 87 4/4/2025
0.1.0-alpha.33 125 4/4/2025
0.1.0-alpha.29 129 4/3/2025
0.1.0-alpha.28 124 4/3/2025
0.1.0-alpha.27 127 4/3/2025
0.1.0-alpha.26 130 4/2/2025