Last active
March 30, 2021 12:35
-
-
Save merken/a3b9ba335a8b3b99e410580d3ba28e1b to your computer and use it in GitHub Desktop.
FooService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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