Skip to content

kryptobi/invoce-custom-code-api

Repository files navigation

Project Overview

This project is a C# web application built with ASP.NET Core. It's designed to handle custom responses based on the route accessed by the client. The project uses a middleware to determine the correct method to call based on the route.

Middleware

The Middleware is a key component of this project. It's responsible for intercepting HTTP requests and determining the correct method to call based on the route. Here's a simplified example of how a middleware might look:

public class Middleware
{
    private readonly RequestDelegate _next;

    public Middleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context, ICreateResponseUseCaseProvider provider)
    {
        var useCase = provider.GetUseCase(context.Request.Path);
        var response = useCase.Create();

        await context.Response.WriteAsync(response);
    }
}

In the InvokeAsync method, the middleware retrieves the correct use case from the ICreateResponseUseCaseProvider based on the request path. It then calls the Create method on the use case to generate the response.

Use Case Provider

The ICreateResponseUseCaseProvider is an interface that defines a method for retrieving the correct use case based on the request path. An implementation might look like this:

public class CreateResponseUseCaseProvider : ICreateResponseUseCaseProvider
{
    private readonly IServiceProvider _serviceProvider;

    public CreateResponseUseCaseProvider(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public CreateResponseUseCase GetUseCase(string path)
    {
        return path.Contains("custom") 
            ? serviceProvider.GetRequiredService<CreateCustomResponseUseCase>() 
            : serviceProvider.GetRequiredService<CreateResponseUseCase>();
    }
}

In the GetUseCase method, the provider determines the correct use case to return based on the request path. This allows the middleware to call the correct method to generate the response.

API

The API is defined in the Startup class. It sets up the middleware, configures the services, and defines the routes. Here's a simplified example of how the ConfigureServices method might look:

public void Configure(WebApplication app, IWebHostEnvironment env)
{
    // ...

    app.UseMiddleware<CustomCodeRegisterMiddleware>();

    // ...
}

public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddTransient<CreateCustomResponseUseCase>();
    services.AddTransient<CreateResponseUseCase>();

    services.AddSingleton<ICreateResponseUseCaseProvider, CreateResponseUseCaseProvider>();

    // ...
}

In this method, CreateCustomResponseUseCase and CreateResponseUseCase are added as Transient services. This means that they are created anew for each request. This allows the Use Cases to operate independently and without side effects. The ICreateResponseUseCaseProvider is added as a Singleton, as it is stateless and its instance can be reused between requests.

Conclusion

This project demonstrates a flexible way to handle custom responses based on the route accessed by the client. By using a middleware and a use case provider, the application can easily determine the correct method to call to generate the response.

About

An example how to invoce custom code via a middleware in a aspnet core web api

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages