Skip to content

Instantly share code, notes, and snippets.

@merken
Last active March 30, 2021 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merken/a3b9ba335a8b3b99e410580d3ba28e1b to your computer and use it in GitHub Desktop.
Save merken/a3b9ba335a8b3b99e410580d3ba28e1b to your computer and use it in GitHub Desktop.
FooService
using System;
using FuncR;
using Microsoft.Extensions.DependencyInjection;
namespace Your.First.Function
{
// We don't need no, implementation
public interface IFooService
{
string Foo(int numberOfFoos);
}
class Program
{
static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddScopedFunction<IFooService>
(nameof(IFooService.Foo))
// string Foo(int numberOfFoos)
.Runs<int, string>(numberOfFoos =>
{
if(numberOfFoos >= 5)
return "Too many foos!";
return $"This many foos: '{numberOfFoos}'";
});
var fooService =
services.BuildServiceProvider().GetRequiredService<IFooService>();
// Prints: "This many foos: '3'"
Console.WriteLine(fooService.Foo(3));
// Prints: "Too many foos!"
Console.WriteLine(fooService.Foo(5));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment