Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow class to be used for function #7

Closed
Daniel15 opened this Issue Apr 8, 2019 · 3 comments

Comments

Projects
None yet
1 participant
@Daniel15
Copy link
Owner

Daniel15 commented Apr 8, 2019

Currently we use:

FdkHandler.Handle(async (ctx, input) => 
{
    // Do stuff
});

ASP.NET Core allows middleware to be defined either as a lambda function (similar to what we do here), or as a class. When using a class, it handles injecting dependencies both through the constructor (for singletons) and through the method call itself (for transient and scoped dependencies)

This would potentially allow for a cleaner API, where we can just determine whether the input is a string, a stream, or JSON just based on the function's argument type, rather than having to call AsString etc. explicitly.

Potential API:

public class MyFunction
{
    // Example of dependency injected via constructor
    private readonly ILogger<MyFunction> _logger;
    public MyFunction(ILogger<MyFunction> logger)
    {
        _logger = logger;
    }

    // Input is automatically passed in as a string
    // `IFoo` could be some scoped dependency
    public Task<string> InvokeAsync(string input, IFoo foo)
    {
        return "Hello " + input;
    }
}

This looks similar to what the Java FDK does.

In Program.cs, we could have an init API like this:

FdkHandler.Handle<MyFunction>();

Code reference for how middleware handles it: https://github.com/aspnet/AspNetCore/blob/master/src/Http/Http.Abstractions/src/Extensions/UseMiddlewareExtensions.cs#L145

@Daniel15 Daniel15 self-assigned this Apr 8, 2019

@Daniel15

This comment has been minimized.

Copy link
Owner Author

Daniel15 commented Apr 11, 2019

Started working on this, basic pieces are there in master, but it's not complete yet.

@Daniel15

This comment has been minimized.

Copy link
Owner Author

Daniel15 commented Apr 12, 2019

TODO: Inject CancellationToken

@Daniel15

This comment has been minimized.

Copy link
Owner Author

Daniel15 commented Apr 13, 2019

Implemented in 1.0.0 release.

@Daniel15 Daniel15 closed this Apr 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.