Created
December 21, 2018 20:58
-
-
Save tonysneed/3e884440afc8602ce047d8ff0f780bf4 to your computer and use it in GitHub Desktop.
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
public class Function | |
{ | |
// Repository | |
public IProductRepository ProductRepository { get; } | |
public Function() | |
{ | |
// Get dependency resolver | |
var resolver = new DependencyResolver(ConfigureServices); | |
// Get products repo | |
ProductRepository = resolver.ServiceProvider.GetService<IProductRepository>(); | |
} | |
// Use this ctor from unit tests that can mock IProductRepository | |
public Function(IProductRepository productRepository) | |
{ | |
ProductRepository = productRepository; | |
} | |
/// <summary> | |
/// A simple function that takes an id and returns a product. | |
/// </summary> | |
/// <param name="input"></param> | |
/// <param name="context"></param> | |
/// <returns></returns> | |
public async Task<Product> FunctionHandler(string input, ILambdaContext context) | |
{ | |
int.TryParse(input, out var id); | |
if (id == 0) return null; | |
return await ProductRepository.GetProduct(id); | |
} | |
// Register services with DI system | |
private void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddTransient<IProductRepository, ProductRepository>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment